How to align memory in the structure: take you to learn the case of 1+4=8

In the knowledge we usually do, we have until the char size is 1, short is 2, and int is 4.

But when in the structure?

Look at the following piece of code:

struct A {
    int a; 
    char b; 
    short c; 
};

The normal structure size algorithm should not be: 4+1+2=7

But the actual running result is: 8 ;


The reason why the above result is produced is that if it is stored in a normal size, the efficiency of the system will be very low.

Because when the CPU reads data from the memory, it reads one byte by itself. If it is accessed according to the normal size, the following situation will occur.

When reading the first byte of char, the first four bytes are read, and then the first byte is taken.

When reading the second int, it will read the first four bytes first, then remove the first bit, then read the next four bytes,
       and then add down the last three of the four bytes If the bits are removed and combined into one piece, the resource utilization rate is low and it is troublesome.

Therefore, this method obviously has many shortcomings, so a memory alignment situation occurs.

Memory alignment

It is to put a specific type of variable at a specific address, which requires each variable to be arranged in space according to a certain rule, rather than simply in order. This is memory alignment.

Memory alignment rules

First of all, we have to know a few concepts:

  1. The alignment value of the data type itself: it is the alignment value of the basic data type explained above.
    For char type data, its self-alignment value is 1, for short type it is 2, and for int, float, double type, its self-alignment value is 4, unit byte.

  2. Specify the alignment value: #pragma pack (value) specifies the alignment value value.
    You can use #pragma pack (show) to display the system-defined value, or the value can be defined by yourself (the system defaults to 8, you can try it yourself)
  3. The self-alignment value of a structure or class: the value with the largest self-alignment value among its members.
  4. Valid alignment values ​​of data members, structures, and classes: the smaller of its own alignment value and the specified alignment value

The first of these four is the normal self-alignment value. Compare this value with the specified alignment value. The smaller one is the effective alignment value of the data.

Then the following three items are mainly about the effective alignment value of the structure: first find the maximum value of the structure itself, and then compare it with the specified alignment value. The two smaller ones are the effective alignment value of the structure.

After understanding the above four concepts, we only need to remember the storage address:

Store start address% effective alignment value = 0

Mainly these two concepts, and then we analyze the above code.

struct A {     int a; The self-alignment value is 4, and the system default is 8, so the effective alignment value is 4, and the storage address %4=0 must be satisfied, so it is enough to put it in the 0 position. The position occupied is 0     , 1, 2, 3 char b; The alignment value of itself is 1, and the system defaults to 8, so the effective alignment value is 1, which needs to meet the storage address %1=0. At this time, position 4 is satisfied. The position is 4     short c; the self-alignment value is 2, and the system defaults to 8, so the effective alignment value is 2, and the storage address %2=0 must be satisfied. At this time, position 5 is obviously not good, so put it in position 6 and occupy the position 6, 7. }; The total occupies 8 positions. At this time, the maximum size of the structure is 4, and the system is 8, so the effective value of the structure is 4. At this time, 8%4=0, which meets the condition, so the size is 8.



What if the data location in the structure is changed? Is it still 8 at this time?

struct B {
    char b;
    int a; 
    short c; 
};

Let's analyze

struct B {     char b; The self-alignment value is 1, and the system default is 8, so the effective alignment value is 1, and the storage address %1=0 must be satisfied. At this time, position 0 is satisfied, and the occupied position is 0     int a; self-alignment The value is 4, the system default is 8, so the effective alignment value is 4, and the storage address %4=0 is required, but the positions 1, 2, and 3 are not satisfied, so it is placed in position 4, and the occupied positions are 4, 5. 6, 7     short c; The self-alignment value is 2, and the system defaults to 8, so the effective alignment value is 2, which needs to satisfy the storage address %2=0, so it can be placed in the 8 position, occupying the position 8, 9 }; The total occupies 10 positions. At this time, the maximum in the structure is 4 and the system is 8, so the effective value of the structure is 4, at this time 10% 4! =0, does not meet the conditions, so it needs to be adjusted, that is, take the closest to 10 and satisfy %4=0, which is 12, so the size becomes 12 at this time



Do you understand the structure size of the second code of 12?

Mainly master this memory alignment rule.

Advantages of memory alignment

  1. Memory alignment is the strategy used by the operating system to improve memory access. When the operating system accesses the memory, it reads a certain length each time (this length is the default alignment number of the operating system, or an integer multiple of the default alignment number).
    If it is not aligned, a second visit may occur in order to access a variable.
  2. Improve the speed of data access. For example, some platforms read data from an even address every time. For an int type variable, if it is stored from an even address unit,
    it only needs one read cycle to read the variable; but if it is from an odd address If the unit is stored, it takes 2 reading cycles to read the variable.
  3. Some platforms can only be

    table of Contents

    Memory alignment

    Memory alignment rules

    Advantages of memory alignment


    Access a specific type of data at the address, otherwise throw a hardware exception to the operating system

Guess you like

Origin blog.csdn.net/qq_46423166/article/details/112603846