C / C ++ structure memory alignment rules structure memory alignment rules

Structure memory alignment rules

1. What is structure memory alignment

        The structure is not like an array. Different types of data can be stored in the structure. Its size is not simply the sum of the size of each data member. It is limited to the requirement of reading memory, but the storage of each member in memory must be in accordance with It is stored at a certain offset. According to different types, each member must be aligned and stored according to a certain alignment number. Finally, the size of the entire structure should be aligned according to a certain alignment number.

Second, the alignment rules

  1. The first member is at an address that has an offset of 0 from the structure variable.
  2. Other member variables should be aligned to an address that is an integer multiple of the alignment number (alignment number = the compiler's default alignment number and the smaller value of the member size).
  3. The default in Linux is 4, and the default in vs is 8.
  4. The total size of the structure is an integer multiple of the maximum alignment number (each member variable except the first member has an alignment number).

        If a structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment number, and the overall size of the structure is an integer multiple of all the maximum alignment numbers (including the alignment number of the nested structure)

Features:

  1. Each member's offset% own alignment number = 0
  2. The overall size of the structure% The maximum alignment of all members = 0
  3. The alignment number of the structure is the maximum alignment number among the alignment numbers of its own members

3. Examples

// 平台VS2013下(默认对齐数为8)
// 案例一
    struct S1
    {
        char c1;
        int i;
        short s2;
    };
    printf("%d\n", sizeof(struct S1)); // 12
// 案例二
    struct S2
    {
        char c1;
        short s2;
        int i;
    };
    printf("%d\n", sizeof(struct S2)); // 8
  
  
Case One Analysis

        The char type occupies 1 byte, the compiler's default alignment number is 8, then the variable alignment number is 1, the actual offset is 0. The
        int type occupies 4 bytes, the compiler's default alignment number is 8, then the variable alignment Digit 4, the offset should be a multiple of 4, the actual offset is 4
        short type occupies 2 bytes, the compiler's default alignment number is 8, then the variable alignment number 2, the offset should be a multiple of 2, The actual offset is 8. The
        overall alignment number of the structure is the largest of all members. The alignment number is 4 the
overall size of the structure. According to the size of the space occupied by the above data, the structure size is calculated to be 10 bytes.
        According to the alignment rule, it should be aligned to a multiple of 4, the actual size is 12 bytes

Case two analysis

        The char type occupies 1 byte, the compiler's default alignment number is 8, then the variable alignment number is 1, the actual offset is 0, the
        short type occupies 2 bytes, and the compiler's default alignment number is 8, then the variable alignment Number 2, the offset should be a multiple of 2, the actual offset is 2
        int type occupies 4 bytes, the compiler default alignment number is 8, then the variable is aligned to the digit 4, the offset should be a multiple of 4, The actual offset is 4 The
        overall alignment number of the structure is the largest of all members. The alignment number         is the overall size of the 4
structure. According to the size of the space occupied by the above data, the structure size is 8 bytes.
        According to the alignment rule, it should be aligned to a multiple of 4, the actual size is 8 bytes

Graphical analysis

Memory alignment

Fourth, why there is memory alignment

Platform portability is good

        Not all hardware platforms can access data at any address; some hardware platforms can only access certain types of data at certain addresses, otherwise hardware exceptions are thrown, and unaligned boundaries are not directly involved. Read the data.

High CPU processing efficiency

Write a picture description here

As can be seen from the above figure, corresponding to the two storage methods, if the read granularity of the CPU is 4 bytes,

  1. Then for an int type, if it is stored according to memory alignment, the processor only needs to access the memory once to read 4 bytes
  2. If it is not read according to memory, as shown in the above figure, you need to access the memory twice to read a complete int type variable
  3. The specific process is: take out 4 bytes for the first time, discard the first byte, take out 4 bytes for the second time, discard the last three bytes, and then piece together a complete int data .

Structure memory alignment is the practice of exchanging space for time. Improve efficiency

Published 32 original articles · won praise 1 · views 4551

Guess you like

Origin blog.csdn.net/hezhanran/article/details/104558955