Packing nested structs in C++
问题 Using Visual Studio 2017, the following gives... struct AAA // 15 bytes { double d; short s; char a1; char a2; char a3; char s4; char s5; }; struct BBB { AAA d; char a4; }; int main() { std::cout << sizeof(AAA) << "\n"; // gives 16 std::cout << sizeof(BBB) << "\n"; // gives 24 getchar(); return 0; } The Question is... how do I get sizeof(BBB) to be 16. 回答1: Use #pragma pack(push, 1) or #pragma pack(1) to enforce compiler not to line up structure members on 2 byte or 4 byte boundaries which