C-CPP-Programming-in-case-of
Data Structure Alignment 数据结构对齐 #
x86-64 architecture #
Overall rule: any data structure/element should be accessed by CPU with minimum CPU cycles. A 32bit CPU can access 4Bytes in one cycle, while a 64bit one can access 8Bytes. When a memory access is not aligned, it is said to be misaligned (wiki).
Thus, there are two practical rules for us to understand compiler’s padding:
Rule 1: any basic data structure/element/variable should be aligned so that their beginning address is divisible (任何变量的开始地址都能被自己的大小整除,即:当被访问的数据长度为 n 字节时,数据地址为 n 字节对齐。).
e.g. int 开始地址必须是 4 的倍数(如果 sizeof(int)==4);short 的开始地址必须是 2 的倍数;char 开始地址必须是 1 的倍数(任意地址);double 开始地址是 8 的倍数。
Rule 2: (actually extended from 1) considering array of struct, the beginning address of a struct should be an integer multiple of the biggest item in the structure (结构体的开始地址能够被结构体内最大的元素的大小整除,才能保证此结构体的数组中的每个结构体的元素都是对齐的).
e.g.
struct student {
char a;
int b;
char c;
}; // 12 Bytes
ref: 12 Bytes: javatpoint, (bak)
ref: more examples: geeksforgeeks
ref: sizes of data in different OS’s and architecture
other structure or cpu #
Little Endian vs. Big Endian #
Little endian: bytes that have less influence on the entire value are stored in low addresses.
When a host reads to transfer, low addresses are firstly read/accessed. Thus network protocals defines a rule when transfering data (which may cross platforms), therefore, hton, ntoh.
Tip: x86 and ARM are Little Endian, java and most network protocals are Big Endian (thus Big Endian is also called Network Endian).