The C ++ programming language is a very complex application step for memory operations. During the learning process, we need to constantly sum up the application experience from practical programming, to help us understand. Here we will first introduce the C ++ memory Alignment Methods.
- Tips for deleting C ++ container values
- Overview of basic concepts of C ++ set Initialization
- In-depth analysis of C ++ Vector usage
- Basic Application Skills of C ++ Bubble Sorting
- Introduction to C ++ conventional DLL Application
I. Why C ++ memory alignment
The following content is excerpted from Intel Architecture 32 Manual.
To improve program performance, data structures, especially stacks, should be aligned as much as possible on natural boundaries. The reason is that the processor needs to perform two memory accesses to access non-alignment memory; however, alignment memory access only needs one access.
A single-or dual-character operand spans the 4-byte boundary, or a four-character operand spans the 8-byte boundary, which is considered not aligned and thus requires two bus cycles to access the memory. The starting address of a word is odd, but it does not span the word boundary. It is considered to be aligned and can be accessed in a bus cycle.
II. C ++ memory alignment rules
Each compiler on a specific platform has its own default "alignment coefficient" (also called alignment modulus ). Programmers can use the pre-compiled command # pragma pack (n), n =, and 16 to change this coefficient. n is the alignment coefficient you want to specify ".
Alignment rules:
1. Data member alignment rules: the data member of the structure (struct) (or union). The first data member is placed at the position where the offset is 0, in the future, the alignment of each data member is performed according to the value specified by # pragma pack and the smaller value of the data member's length.
2. Overall alignment rules of the structure (or union): After the data members align themselves, the structure (or union) itself also needs to be aligned, alignment is performed according to the value specified by # pragma pack and the smaller value in the maximum data member length of the structure (or union.
3. Combined with 1 and 2, we can infer that when the n value of # pragma pack is equal to or exceeds the length of all data members, the n value will not have any effect.
4. The offset between the start address and the start address of each member variable must be a multiple of the bytes occupied by the variable type.
5. When each member variable is stored, the space is requested in order of appearance in the structure, and the positions are adjusted according to the above alignment. The vacant bytes are automatically filled.
6. at the same time, to ensure that the size of the structure is a multiple of the byte boundary Number of the structure, that is, the number of bytes occupied by the type that occupies the maximum space in the structure, therefore, after applying for space for the last member variable, the vacant bytes will be automatically filled as needed.
3. pragma pack macro
# Pragma pack (n) is provided in VC to set the variable to n-byte alignment. N-byte alignment means the offset of the Start address of the variable. First, if n is greater than or equal to the number of bytes occupied by the variable, the offset must meet the default alignment mode, second, if n is less than the number of bytes occupied by the variable type, the offset is a multiple of n, and the default alignment is not required. The total size of the structure also has a constraint, which is divided into the following two cases: if n is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the space occupied by the largest variable; otherwise, it must be a multiple of n. The following is an example of its usage.
- # Pragma pack (push) // save alignment status
- # Pragma pack (4) // set to 4-byte alignment
- Struct test
- {
- Char m1;
- Double m4;
- Int m3;
- };
- # Pragma pack (pop) // restore alignment
The size of the above structure is 16. Next we will analyze its storage situation. First, we will allocate space for m1, and its offset is 0, which meets our own 4-byte alignment ), m1 occupies 1 byte. Then we start to allocate space for m4. At this time, the offset is 1 and three bytes need to be supplemented. In this way, the offset must be a multiple of n = 4 because sizeof (double) is greater than n ), m4 occupies 8 bytes. Then allocate space for m3. At this time, the offset is 12, which must be a multiple of 4. m3 occupies 4 bytes. At this time, space has been allocated for all member variables. A total of 16 bytes are allocated, which is a multiple of n. If you change # pragma pack (4) to # pragma pack (16), the size of the structure is 24.
The above is an introduction to C ++ memory alignment.