# Pragma pack specifies the alignment of data in the memory,
In the C language, the structure is a composite data type, and its components can be both variables of basic data types (such as int, long, float, and so on, it can also be a data unit of a composite data type (such as an array, structure, and union. In the structure, the compiler allocates space for each member of the structure based on its natural alignment condition. Each member is stored in the memory in the declared order. The address of the first member is the same as that of the entire structure.
(1) # pragma pack (1)
Purpose: adjust the boundary alignment of a struct so that it is aligned in one byte;
# Pragma pack (1) Align the struct in 1 byte mode
Example 1:
Struct sample
{
Char;
Double B;
};
If you do not need to enclose # pragma pack (1) and # pragma pack (), the sample is aligned in the default way of the compiler (the largest member in size ). That is, alignment by 8 bytes (double), sizeof (sample) = 16. Member char a occupies 8 bytes (7 of which are null bytes)
If # pragma pack (1) is used, the sample is aligned with sizeof (sample) = 9 in 1 byte mode (no NULL bytes)
Space-saving. Some fields and structures can be easier to control.
(2) # pragma pack (push, 1)
This is the parameter settings used by the compiler, which are related to the bytes alignment of the struct.
It usually refers to setting the original alignment mode to press the stack and set the new one to 1.
Example 2: Allocation of member spaces in the following structure:
Struct Test
{
Char x1;
Short X2;
Float X3;
Char X4;
};
The first member of the structure X1, whose offset address is 0, occupies 1st bytes. The second member X2 is of the short type, and its starting address must be two byte pairs. Therefore, the compiler fills a Null Byte between X2 and X1. The third member X3 and fourth member X4 of the structure exactly fall on their natural peer address, and no additional bytes are needed before them. In the test structure, the X3 member requires a 4-byte bounded boundary and is the maximum boundary unit required by all the members of the structure. Therefore, the natural boundary condition of the test structure is 4 bytes, the compiler fills in three NULL bytes after Member X4. The entire structure occupies 12 bytes of space. Change the default byte alignment of the C Compiler
By default, the C compiler allocates space for each variable or data unit based on its natural limitations. Generally, you can use the following method to change the default peer condition:
· With the pseudo command # pragma pack (n), the C compiler will align according to n Bytes.
· Use the pseudo command # pragma pack () to cancel the custom byte alignment.
In addition, the following method is provided:
· _ Attribute (aligned (N) to align the structure members to the natural boundary of n Bytes. If the length of a member in the structure is greater than N, the maximum member length is used for alignment.
· _ Attribute _ (packed): cancels the optimization alignment of the structure during compilation and alignment according to the actual number of bytes occupied.
The first method above n = 1, 2, 4, 8, 16... is more common.