Why sizeof(MyStruct) is often larger than the sum of its member variables:
struct UnalignedData {
char a; // 1 byte
// 7 bytes of padding inserted by compiler!
double b; // 8 bytes (must align to 8-byte boundary)
int c; // 4 bytes
// 4 bytes of tail padding!
}; // Total: 24 bytesBy reordering members from largest to smallest (double b; int c; char a;), the total struct size drops to 16 bytes without changing functionality, saving 33% memory and improving CPU cache line efficiency.