可能的重复:
为什么结构体的 sizeof 不等于每个成员的 sizeof 之和? https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member
#include<stdio.h>
struct csie {
char c;
short s;
int i;
double e;
};
struct ceis {
char c;
double e;
int i;
short s;
};
int main(void) {
printf("csie = %d\n", sizeof(struct csie));
printf("ceis = %d\n", sizeof(struct ceis));
return 0;
}
输出是:
CSI = 16
ceis = 24
The 结盟 http://en.wikipedia.org/wiki/Data_structure_alignment的结构不同。
第一个结构:
struct csie {
char c;
short s; // 3 bytes + 1 bytes of padding
int i; // 4 bytes
double e; // 8 bytes
};
struct ceis {
char c; //1 byte + 7 bytes of padding
double e; // 8 bytes
int i; // 4 bytes
short s; // 2 byte + 2 bytes of padding
};
在第一个结构中,char和short可以打包到同一个对齐块中,而在第二个结构中则不能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)