Change the byte alignment of the C compiler by #pragma pack (n)
In C language, the structure is a composite data type, which can be variables that can be both basic data types (such as int, long, float, etc.), or some composite data types (such as arrays, structures, combination, etc.) Data unit. In the structure, each member of the compiler is allocated in a natural alignment condition. Each member is stored in memory in memory in the order in which they are declared, the address of the first member is the same as the address of the entire structure. For example, the following structures each member spatial allocation: struct test {char x1; short x2; float x3; char x4;}; structure's first member X1, its offset address is 0, occupying the first byte . The second member X2 is a short type, and its start address must be 2 bytes of theoretical, so the compiler fills an empty byte between X2 and X1. The third member X3 and the fourth member X4 of the structure are just on its natural parallel address, and there is no additional padding bytes in front of them. In the TEST structure, the member X3 requires 4-byte alignment, which is the maximum parallel unit required in all members of the structure, so the natural relationship condition of the TEST structure is 4 bytes, and the compiler is filled in the member X4. 3 Empty byte. The entire structure is occupied by 12 bytes. Changing the default byte alignment of the C compiler is default, the C compiler allocates space for each variable or data unit. Generally, the default alignment can be varied by the following method: • Use the pseudo command #pragma Pack (N), and the C compiler will be aligned according to n bytes. · Use pseudo-instruction #pragma pack () to cancel the custom byte alignment. In addition, there is a way: · __attribute ((aligned (n))), allowing the active structural members to align the N-byte natural boundary. If the length of the member in the structure is greater than n, it is aligned according to the length of the biggest member. · __Attribute__ ((packed)), the cancellation structure is aligned in the compilation process, and is aligned according to the actual number of bytes. The above N = 1, 2, 4, 8, 16 ... is more common. Application Examples The data packets of different protocols are often processed in the network protocol programming. One way is to get various information by a method of being offset by a pointer, but do not only programmed complex, but once the protocol changes, the program modification is more troublesome. After understanding the allocation principles of the compiler on the structural space, we can use this feature to define your own protocol structure, access the various information by accessing the structure. This is not only simplified programming, but also even if the protocol changes, we only need to modify the definition of the protocol structure, other programs do not need to be modified, save time and effort. The following is the first TCP protocol as an example, explaining how to define protocol structures.