#include <iostream>
union xxx
{
uint64_t f5;
char f4;
struct
{
char f1;
uint32_t f2;
char f3[3];
} abc;
};
union xxx2
{
uint64_t f5;
char f4;
};
struct xxx3
{
char c;
uint32_t c2;
};
struct xxx4
{
char c;
uint32_t c2;
char c3;
};
int main()
{
std::cout << sizeof(union xxx) << std::endl; // 16
std::cout << sizeof(xxx::abc) << std::endl; // 12
std::cout << sizeof(union xxx2) << std::endl; // 8
std::cout << sizeof(xxx3) << std::endl; // 8
std::cout << sizeof(xxx4) << std::endl; // 12
return 0;
}
Question> The size of a union is sufficient to contain the largest of its data members. The largest data member inside xxx is abc which is of size 12 bytes. Why the sizeof xxx is 16 instead of 12?