返回信息流#pragma pack(8)
struct example1
{
short a;
long b;
};
struct example2
{
char c;
example1 struct1;
short e;
};
#pragma pack()
int main(int argc, char* argv[])
{
example2 struct2;
cout << sizeof(example1) << endl;
cout << sizeof(example2) << endl;
cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2) << endl;
return 0;
}
问程序的输入结果是什么?
答案是:
8
16
4
对吗?
这是一条镜像帖。来源:北邮人论坛 / cpp / #41907同步于 2010/7/29
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
有关struct 的题
Wyatt
2010/7/29镜像同步2 回复
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
我的印象是
structure中的structure成員的alignment是按照structure成員中的最大的成員來看的
也就是說
struct example2
{
char c;
example1 struct1;
short e;
};
struct1的是按照其內部的long來對齊的 也就是4
所以最終結果就8 16 4
在32位機器上