1、数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员存储的起始位置要从该成员的大小或子成员大小的整数倍开始存储(比如int 为4字节,那么他的起始位置则为4*n)
2、结构体作为成员:如果一个结构里有某些结构成员,则结构成员要从其内部最大元素大小的整数倍地址开始存储
3、结构体总体大小,必须是其内部最大成员的整数倍,不足要补齐
案例
typedef struct one {
char a; // length 1 offset 0 position 0
int b; // length 4 offset 4 * n position 4567
double c; // length 8 offet 8 * n positon 8~15
char d; // length 1 offset 1* n position 16
// total currentLength 17 遵循规则3 最大的整数倍 8*n result 24
} one;
typedef struct two {
char a; // length 1 offset 0 position 0
int b; // length 4 offset 4 * n position 4~7
double c; // length 8 offet 8 * n positon 8~15
char d; // length 1 offset 1* n position 16
one e; // max (8) offset 8*n(原则2) position 24 ~ 41
// total currentLength 41 遵循规则3 最大的整数倍 8*n result 48 } two;
输出运行结果