【发布时间】:2015-01-13 20:18:36
【问题描述】:
#include <iostream>
using namespace std;
#pragma pack(push, 4)
struct Foo
{
char ch; //1
char ch2; //1
char ch3; //1
char ch4; //1 _4
char ch5; //1
short num; //2
char ch6; //1 _4
int num2; //4 _4
};
#pragma pack(pop)
int main() {
cout << sizeof( Foo );
return 0;
}
为什么输出是 16 字节?我认为它必须是 12,因为:
4 char = 4 bytes
char + short + char = 4 bytes
int = 4 bytes
那么有人能解释一下剩下的 4 个字节在哪里吗?
【问题讨论】:
-
short不仅在您的实现中是 2 大,它还具有对齐 2。
标签: c++ windows visual-studio-2010