C语言内存地址对齐

来源:互联网 发布:软件测试书籍推荐 编辑:程序博客网 时间:2024/05/12 15:37

预处理命令#pragma pack(1) 可以使C语言中变量以1字节对齐,#pragma pack()这样就可以取消,重新以默认方式对齐。

 

struct st1

{

 int a;

char c;

char c2;

};

 struct st2

{

char c;

int a;

char c2;

};

sizeof(st1)=? 

sizeof(st2)=?

 

在32位系统,VC2010编译器中答案是sizeof(st1)=8,sizeof(st2)=12;如果变成下面这样:

#pragma pack(1)

struct st1

{

int a;

char c;

char c2;

};

struct st2

{

char c;

int a;

char c2;

};

sizeof(st1)=?

sizeof(st2)=?

 

那么两个的大小都是6.

原创粉丝点击