内存地址对齐 sizeof
2010年05月11日 星期二 上午 08:48
内存地址的对齐主要考虑三个因素: 1:对于每个成员的起始地址是他本身所占的整数倍 2:整个所占的内存是成员中占的地址内存最多的整数倍 3:有#pragma pack(int)进行设置,如 果结构体某成员的sizeof大于你设置的,则按你的设置来对齐 注意:每次用#pragma pack(int)进行设置后,要用#pragma pack()对其结束,免得造成错误
注意下面我对#pragma pack() 用法的理解,他不仅对每个成员的起始地址起作用,对整个的也起作用。请看下面的测试程序
下面接受有结构体嵌套的时候: 对于整个结构体作为成员函数时,他的起始地址不是以该结构体整体作为标准,而是以其内的成员为标准
最后注意:static成员和函数其实是类层次的,不在对象中分配空 间,而成员函数其实是被编译为全局函数了,所以也不在对象中。
|