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.
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 makes it easier and faster for the processor to handle. So the structure contains secret padding bytes to make this happen. But this increases memory usage because of padding.
Its a precise explanation here