definition
|
|
typedef defines “gid” as an alias name to “struct gid_ “. typedef also alias function handle/pointer, which is often used in asynchronous/event callback programming. other data encapsulation are:
enum : map a list of const names to integral constants
union: store many data type in one memory address
initialization
|
|
in C++, structure initialization can also be done in constructor.
memory alignment
for better memory access in CPU architecture, memory alignment in structure is considered. namely:
chars can start on any byte address
2-bytes shorts must start on an even address
4-bytes ints/floats must start on an address divisible by 4
8-bytes doubles/longs must start on an address divisible by 8
the size of the whole structure, is aligned to intergeral times of the max size of its member variable. e.g. sizeof(gid) = 24, not 8+8+4.
to put the member variables in ascending/descending order is good practice.
structure pointer arithmetic
“gid++” will step forward the sizeof(gid); structure also supports self-reference:
|
|
another common utils is structure array:
|
|
structure as function parameters
in general, structure can be passing to function by value or by pointer, but not by reference in pure C. also structure as return value from function can be value or a pointer
structure in C++
in C++, structure supports member functions, is same as a public class. and the initialization can be done either in constructor function or direct initialization during definition. see the difference of struct between C and C++
stdlib.h
|
|