เพราะ padding และ alignment (การจัดแนวหน่วยความจำ)
Processor แต่ละสถาปัตยกรรมมี word size (32-bit = 4 bytes, 64-bit = 8 bytes) ตัวแปรแต่ละชนิดมี alignment requirement
ตัวอย่าง
struct X {
char b[3]; // 3 bytes
int c; // 4 bytes, ต้อง align ที่ 4-byte boundary
};
// sizeof(b) + sizeof(c) = 3 + 4 = 7
// แต่ sizeof(X) = 8 (เพิ่ม 1 byte padding หลัง b[3])
ผลลัพธ์บน 64-bit
8 7
Compiler ใส่ padding bytes ระหว่าง members เพื่อให้แต่ละ member อยู่ที่ address ที่หารด้วย alignment requirement ลงตัว
อ่านเพิ่ม: Data Structure Alignment (Wikipedia)