问题描述
输入以下代码:
#include <iostream>
using namespace std;
int main() {
struct people {char name[50]; int tt; int gg;};
struct people p1;
cout << sizeof(p1);
return 0;
}
输出为60。为什么?struct
仅需要50 + 4 + 4 = 58个字节。如果将name[50]
更改为name[20]
,我们将得到输出:28如预期。
与结构成员使用的内存是连续的并不矛盾?。
解决方法
为了提高效率(主要是执行速度),编译器会将Start-Process $dest
变量对准4字节边界。因此,您的结构的布局为:
int