结构的每个成员的类型通常具有默认对齐方式,即每个结构成员在预先确定的边界上对齐。出于这个原因,填充是在以下 wiki 示例中执行的:
struct MixedData
{
char Data1;
short Data2;
int Data3;
char Data4;
};
struct MixedData /* After compilation in 32-bit x86 machine */
{
char Data1; /* 1 byte */
/* 1 byte for the following 'short' to be aligned on a 2 byte boundary
assuming that the address where structure begins is an even number */
char Padding1[1];
short Data2; /* 2 bytes */
int Data3; /* 4 bytes - largest structure member */
char Data4; /* 1 byte */
char Padding2[3]; /* 3 bytes to make total size of the structure 12 bytes */
};
应该保留对齐的(实际)原因是什么?