Structure boundary alignment and structure boundary alignment
The structure boundary is a conventional topic. There are many explanations on the Internet, but most of them focus on the steps. The reasons for each step are a bit unclear, the following describes the structure Alignment Based on the online explanation and your understanding, which is not necessarily correct.
1. What is structure alignment.
Struct {
Char;
Char B;
Int c;
Char d;
}
For the above struct, assuming that the machine font is 32 characters long (4 bytes), the space occupied by the struct variable is not 1 + 1 + 4 + 1 = 7, but 1 + 3 + 4 + 1 + 3 = 12. The compiler automatically adjusts the starting position of c to 4th bytes, and adds three bytes to the final position of the struct. This compiler adjusts the memory address of the variable, that is, the structure alignment.
2. Why structure alignment.
The addressing of the cpu is not random access. We regard a byte as a memory unit, and add continuous labels to each memory unit to form a memory address, cpu access to memory depends on the machine font length. Generally, if the machine font length is N, when the cpu accesses the memory, it reads N consecutive memory units whose IP address is an integer multiple of N (this seems to be reflected in the cpu addressing module of the Computer composition principle). Therefore, take struct A as an example. If the storage location of the member variable c is not adjusted and the storage is still started from 3rd bytes, the cpu needs to address it twice to access, the first access address is 0 for four consecutive memory units, the value of the last two units is part of c, and then the access address is 4 for four consecutive memory units, the first two units are part of c. If the memory is aligned and the variable c is placed at an integer multiple of the machine's font length, you only need to access it once.
3. Think a little.
The alignment method mentioned above does not mention that, since the variable c is placed at the integer multiple of the machine's font length, why does variable B not do this, just store all the variables by an integer multiple of the machine's font length. In my understanding, when accessing a memory unit, the biggest overhead is the occupation and transmission of the bus. As for the internal processing of data in the cpu, the overhead is almost non-existent, for a variable whose length is less than the machine's font length, the storage location must be an integer multiple of its own length, in this way, the cpu can obtain all the data at one memory access (this is the case, because all the basic variables, including the machine font length, are the relationship between Division and Division, the length rules are 1, 2, 4, 8, 16, 32 ......, Although it will not be strictly proved, but can not think of the results of meeting this rule, there are two reverse examples of access), and then the cpu can be extracted through a very small cost, in this way, the time cost is only a little more than the storage by integer times of the machine's font length (because data can be obtained only by one access to the memory, and the cpu's internal processing cost is very small ), but it saves storage space. If there are many variables, the space saved is considerable.
4. Summarize the steps for alignment of struct members
Determine the machine font length. each variable in the structure is longer than the machine font, and the shorter value is used as the alignment of its coefficients. Pay attention to this point, finally, we need to round the length of the longest variable in the structure and the smaller number in the machine length to ensure that each array element can be aligned when the structure array is used.