Three basic principles
In 1.struct or union or class, the alignment start bit of each member must be an integer multiple of the member's own size;
2. The size of each structure must be an integer multiple of the largest member of the structure, (the second way)
3. The starting bit of struct B within struct a must be an integer multiple of the largest member in B.
Example one:
typedef struct BB {
int id; [0] .... [3]
Double weight; [8] ..... [15] Principle 1
float height; [16]. [19], the total length to 8 of the integer times, padded [20] ... [23] Principle 3
} ;
The result is: 24
Example two:
typedef struct aa{
Char name[2]; [0],[1]
int id; [4] ... [7] Principle 1
Char Score[8]; [8] .... [15]
Short grade; [16],[17]
BB b; [24] ... [47] Principle 2 (the largest member of BB is 8 bits.) So starting from the starting bit from 24)
int A; [48] ... [51] The total length of a multiple of 8 is padded [52] ... [55]
} ;
The result is: 56
Reference: http://blog.csdn.net/hairetz/article/details/4084088
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
byte alignment issues in C + +