您的位置:首页 > 其它

#pragma pack(n)的use(看编辑)

2016-06-21 11:12 225 查看
#include <stdio.h>
#include <stdlib.h>
#pragma pack(8)
struct S1
{
short a;
long b;
};

struct S2
{
char c;
struct S1 d;
double e;
};

int main(void)
{
struct S2 s20;

printf("sizeof(struct S1) = %d\n",sizeof(struct S1));

printf("sizeof(struct S2) = %d\n",sizeof(struct S2));

printf("(int)&s20.d-(int)&s20.c = %d\n",(int)&s20.d-(int)&s20.c);

printf("(int)&s20.e-(int)&s20.d = %d\n",(int)&s20.e-(int)&s20.d);

printf("Hello World!\n");
return 0;
}


输出:



S1

变量 类型大小 pack 起始地址 大小 空出

a 2 8 0 2[0,1]

2[2,3]

b 4 8 4 4[4,5,6,7]

S2

变量 类型大小 pack 起始地址 大小 空出

c 1 8 0 1[0]

3[1,2,3]

d 4 8 4 8[4,5,6,7,8,9,10,11]

4[12,13,14,15]

e 8 8 16 8[16,17,18,19,20,21,22,23]

结构体数据类型,就不能取其大小,而要取结构体中用过的对齐数中最大的一个,即4。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: