struct EMP
{
char apple;//1
int orange;//0001111
short pear;//1100
}example;
typedef struct
{
int aa1; //4个字节对齐 1111
char bb1;//1个字节对齐 1
short cc1;//2个字节对齐 011
char dd1; //1个字节对齐 1000
} testlength1;
int length1 = sizeof(testlength1); //4个字节对齐,占用字节1111 1011 1000,length = 12
typedef struct
{
char bb2;//1个字节对齐 1
int aa2; //4个字节对齐 0001111
short cc2;//2个字节对齐 11
char dd2; //1个字节对齐 10
} testlength2;
int length2 = sizeof(testlength2); //4个字节对齐,占用字节1000 1111 1110,length = 12
typedef struct
{
char bb4; //1个字节对齐 1
char dd4; //1个字节对齐 1
short cc4;//2个字节对齐 11
int aa4; //4个字节对齐 1111
} testlength4;
int length4 = sizeof(testlength4); //4个字节对齐,占用字节1111 1111,length = 8
typedef struct
{
char bb3; //1个字节对齐 1
char dd3; //1个字节对齐 1
int aa3; //4个字节对齐 001111
short cc23;//2个字节对齐 1100
}testlength3;
int length3 = sizeof(testlength3); //4个字节对齐,占用字节1100 1111 1100,length = 12
int _tmain(int argc, _TCHAR* argv[])
{
/*printf("%x/n", &example.apple);
printf("%x/n", &example.orange);
printf("%x/n", &example.pear);
printf("size of struct=%d/n", sizeof(example));*/
printf("length1 = %d.\n", length1); //12
printf("length2 = %d.\n", length2);
printf("length3 = %d.\n", length3);
printf("length4 = %d.\n", length4);
system("pause");
return 0;
}
浙公网安备 33010602011771号