结构体大小问题

来源:互联网 发布:大数据挖掘软件 编辑:程序博客网 时间:2024/05/17 06:39
struct test{int i;char c;float f;};

sizeof(test) = 12, 由于存在字节对齐的问题,在c之后会填充三个字节来完成字节对齐,在这个结构体中是四字节的整数倍

#pragma pack(1)struct test{int i;char c;float f;};#pragma pack()

sizeof(test) = 9; pragma pack(#) #指定了字节对齐的时候填充的方式,1表示不进行填充

<pre name="code" class="cpp">__declspec(align(32))struct test{int i;char c;float f;};

sizeof(test) = 32; 获取字节对齐之后的大小和__declspec(align(#)) 取其中最大的值作为结构体的大小

__declspec(align(8))struct test{int i;char c;float f;};
sizeof(test) = 16


0 0
原创粉丝点击