程序中可以指定变量在内存中的对齐方式,按照字节对齐、4字节对齐、8字节对齐等。使用如下命令对:
#pragma pack(push, 4)
#pragma pack(pop)
可以看看下面的程序会输出什么?
#include
#pragma pack(push, 4)
struct a
{
short v1;
int v2;
};
struct b
{
short v1;
short v2;
char c;
};
struct c
{
struct a v3;
struct b v4;
};
int main()
{
printf("%d\n", sizeof(short));
printf("%d\n", sizeof(char));
printf("%d\n", sizeof(struct a));
printf("%d\n", sizeof(struct b));
printf("%d\n", sizeof(struct c));
}
#pragma pack(pop)