Structure Boundary alignment

Source: Internet
Author: User

Structure boundary to it is a commonplace topic, the online interpretation is very much, but most of the emphasis on the steps, for each step of the reasons are a little unclear, the following combination of online explanations and their understanding of the structure of the alignment, not necessarily correct.

1. What is the structure alignment?

struct a{

Char A;

Char b;

int C;

Char D;

}

For the above structure, assuming that the machine word length 32 bits (4 bytes), then the structure of the variable occupied space is not 1+1+4+1=7, but 1+3+4+1+3=12. The compiler automatically adjusts the starting position of C to the 4th byte and adds 3 bytes at the end of the struct, and the compiler adjusts the variable memory address to the structure alignment.

2. Why should the structure be aligned?

CPU addressing is not random access, we think of a byte as a memory unit, to add a continuous label for each memory unit, constitute the memory address, CPU access to memory depends on its machine word length, generally speaking, if the machine word length is n, That CPU accesses memory each time is to read the integer number of n as the address of a continuous n internal storage element (this seems to be in the computer composition principle CPU addressing that block also reflects), so, in the case of struct A, if the variable of the member variable C storage location is not adjusted, still starting from the 3rd byte to store, Then the CPU to access a will be addressed two times, the first access to the address of 0 consecutive 4 memory units, the value of the next 2 cells is part of C, and then access to 4 memory units with the address 4, the first 2 units are part of C. In case of memory alignment, the variable c is placed in the position of the machine Word integer multiples, only one visit is required.

3. A little thought.

The above-mentioned alignment method does not mention that since the variable C is placed in the position of the integer multiple of the machine word length, why the variable B does not do so, all the variables are stored in terms of the machine word integer multiples of the line. My understanding is that when accessing the memory unit, the maximum overhead is the consumption and transmission of the bus, as for the processing of data within the CPU, compared to the cost of almost no, so for the length of the variable is less than the machine word size, its storage location as long as the length of the integer multiples can be, This ensures that the CPU can get all the data once in memory access (it feels like this, because all the underlying variables, including machine word lengths, are divisible and divisible, the length rule is 1,2,4,8,16,32 ..., though not strictly proven, But can not think of satisfying the rule results are also divided into two times to access the counter example, and then the CPU within a very small cost to extract it, so the time cost compared to the machine word integer multiple storage only a little bit (because it is only one memory access to get the data, and the CPU internal processing cost is very small) , but save the storage space, imagine if a lot of variables, then the space saved is very considerable.

4. Summarize the steps of the structure member alignment

To determine the machine word length, the structure of each variable and machine word length comparison, take a shorter value as their respective coefficients to align, here to pay attention to, and finally to take the structure of the longest variable length and machine word size of the smaller number of the structure of the body to be rounded, This is to ensure that each array element is aligned when using the struct array.

Structure Boundary alignment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.