typedef struct {
WORD dlgVer;
WORD signature;
DWORD helpID;
DWORD exStyle;
DWORD style;
WORD cDlgItems;
short x;
short y;
short cx;
short cy;
} DLGTEMPLATEEX;
这个结构体的长度(size,即sizeof的结果)是多少?我觉得应该是2+2+4+4+4+2+2+2+2=24
但sizeof的结果却是28,可以告诉我是为什么吗?
顶一下
字节对齐的问题。
在编译时加入#pragma pack(1)就是你想要的结果了。
字节对齐
typedef struct {
WORD dlgVer; 2
WORD signature; 2
DWORD helpID; 4
DWORD exStyle; 4
DWORD style; 4
WORD cDlgItems; 2
short x; 2
short y; 2
short cx; 2
short cy; 2
} DLGTEMPLATEEX;
加起来为26,又因为结构体中DWORD所占长度最长,按字节对齐,结构体长度要为DWORD的整数倍,即4的倍数,所以补齐为28
关于字节对齐论坛上有好多帖子的,搜一下就知道了
不要老自己觉得好不好
机器只能机器识别D
注意字节对齐的问题
是啊~~
这个问题也是一个老问题~~
经常有人问~~
就是关于字节对齐问题~~
只是我收藏的几个,可以参考一下:
http://community.csdn.net/Expert/topic/4196/4196234.xml?temp=.3541529
http://community.csdn.net/Expert/topic/4171/4171624.xml?temp=.6486475
顶
哈哈,楼主算错了,都加起来是26字节,而VC下struct默认是4字节对齐的,所以有2字节的pad,就是28字节咯