1: What is memory alignment

Let’s start with a small example:

struct {
    int x;
    char y;
} s1;

int main(int argc, const char * argv[]) {

    printf("%lu\n", sizeof(s1));
    
 }
Copy the code

The output is: 8

Int = 4 byte, char = 1 byte, int = 4 byte, char = 1 byte But the structure is 8 bytes, which is due to byte alignment.

Memory space in the modern computers are divided according to the byte, theoretically seems to access to any type of variables can be from any address, but the actual computer system of the basic types of data in memory to store the location of the limited, they can ask the data of the first address is the value of k of a certain number (usually it is 4 or 8) multiples, This is known as memory alignment.

2: Why memory alignment

For faster memory acquisition

When actual memory is read, every n memory units are grouped and one group is read at a time. N is called the alignment coefficient.

Each platform-specific compiler has its own default “alignment coefficient” (also called alignment modulus). Programmers can change this coefficient by precompiling the command #pragma pack(n), n=1,2,4,8,16, where n is the “alignment coefficient” you want to specify. Take the structure above, for examplen=41, 2, 3, 4 store int x, 5 store char Y. As shown in the lower body

  • When you read x, you just take out the first set of memory numbers 1 through 4 and read x;

  • When reading y, take out memory 5 to 8, and then buckle out memory 5 to read out;

So what ifn=2As shown in the figure below:xYou have to read it first1, 2,Number memory, read again3, 4And then they put it back together. This results in two reads of memory, which is slow for the CPU to access, and two reads wastes a lot of time.

3: Alignment rules

  • Data member alignment rule :(a data member of a Struct or Union) the first data member is placed at offset 0. In the future, the position of each data member is an integer multiple of min (alignment coefficient, its own length), and the next position is not the automatic completion of the integer multiple position of this data member.

  • A data member is a structure: the location where the data member is stored as an integer multiple of the maximum length within the data member.

  • Overall alignment rule: After data members are aligned in one or two steps, they should also be aligned themselves. The alignment rule is an integer multiple of min (the alignment coefficient, the maximum length of data members).

struct {
    double a;

    char b;

    int c;
    
    short d;
} s1;

struct {
    double a;

    int c;

    char b;

    short d;
} s2;

int main(int argc, const char* argv[]) {

    printf("%lu\n", sizeof(s1));

    printf("%lu\n", sizeof(s2));
  
}
Copy the code

Output: s1 is 24, s2 is 16

S1 memory structure:

  • A isdoubleType, 8 bytes of space, offset 0
  • B ischarType, 1 byte space. As a rule, the offset of b is an integer multiple of the size of char. The current offset is 8, which is an integer multiple of its offset
  • C isintAccording to rule 1, the offset of 4 must be an integer multiple of int, so the compiler inserts a three-byte buffer after b
  • D isshortType, takes up 2 bytes of space, and the offset of d is exactly 16 bytes
  • The size of s1 is 18 bytes. According to rule 3, s1 must be an integer multiple of the size of its largest member, double24

S2 memory structure:

References:

  • zhuanlan.zhihu.com/p/30007037
  • Juejin. Cn/post / 697173…