Simple understanding of structure memory alignment and memory in C language

Article directory

  • Understanding of memory units
  • Rules for memory alignment in structures
  • Why there is memory alignment


First, the understanding of the memory unit

First of all, let's introduce some common storage units in C language

    bit stores a binary
    bit Byte 1Byte = 8 bit
    KB 1KB = 1024 Byte
    MB 1MB = 1024 KB
    GB 1GB = 1024 MB
    TB 1TB = 1024 GB
    PB 1PB = 1024 TB

        The size of a memory unit occupies one byte (Byte). The memory unit is a continuous space, and its number is also continuous.

As shown below:

        Next, let's explore how the address number is generated:

        In fact, on our computer, there is such a physical wire called an address line. Once the address line is powered on, it will generate an electrical signal, forming a high level or a low level. The high and low level signals can be converted into digital signals, which correspond to 1 or 0. There are 32 address lines on the 32-bit machine. After the 32 address lines are powered on, the 32 level signals can be converted into the corresponding 32-bit 01 binary sequence. .

         As can be seen from the figure above, there is a total of one address number generated by 32 address lines 2^{32}. Assume that 32 0 sequences are used to manage the memory unit pointed to by the number 0 in the above figure, and the sequence composed of 31 0 sequences and the last 1 is used to manage the memory unit pointed to by number 1, and so on. In this way, digital signals converted from electrical signals generated on physical wires can be used to manage memory cells. In other words, a 32-bit machine can manage 2^{32}a Byte-sized memory, that is, a 4GB-sized memory.

Second, the rules of memory alignment in the structure

1. The first member is at the address at offset 0 from the structure.


2. Other member variables should be aligned to an address that is an integer multiple of a certain number (alignment number). Note: Alignment = the smaller value between the compiler's default alignment and the member size. (The default alignment number in VS is 8)


3. The total size of the structure is: an integer multiple of the maximum alignment number (the largest of all variable types and the smallest default alignment parameter).


4. If a structure is nested, the nested structure is aligned to an integer multiple of its own maximum alignment, and the overall size of the structure is the integer of all maximum alignments (including the alignment of the nested structure) times.

 Below I will explain these four rules:

1. The first member is at the address at offset 0 from the structure.

First of all, we should understand what is the offset, see the figure below:


        Assuming that the starting position of s1 created by structure S1 in the memory is the horizontal line pointed to in the figure, then the offset of the first storage unit below it is 0, and the first member of s1 is from the offset The storage unit with a shift of 0 starts to store.

        2. Other member variables should be aligned to an address that is an integer multiple of a certain number (alignment number). Note: Alignment = the smaller value between the compiler's default alignment and the member size. (The default alignment number in VS is 8)

        As shown in the figure: c1 is the first member of s1, stored at offset 0, the second member i of s1 is of type int, with a size of 4 Bytes, because 4 is less than 8, so the variable i It should be stored from an offset that is a multiple of 4, storing 4 Bytes. Finally, the size of c2 is 1, and the storage starts from the multiple of the offset of 1, that is, it can be stored just after the i variable.

3. The total size of the structure is: an integer multiple of the maximum alignment number (the largest variable type and the default alignment parameter are the smallest).

        Take S1 in the above figure as an example. The types of its three members are char, int, char, and the type sizes are 1, 4, and 1 respectively. Therefore, the largest variable type of all variables is int, and its size is 4Byte, which is smaller than that of VS. The default alignment parameter (8Byte), so the maximum alignment number is 4, that is, the size of the created structure variable s1 must be a multiple of 4. It can be seen from the figure above that s1 has already occupied 9 Bytes at this time, but it is necessary to apply for a space of 3 Bytes from the memory to form a space of 12 Bytes, that is, the size of s1 occupies 12 Bytes.

4. If a structure is nested, the nested structure is aligned to an integer multiple of its own maximum alignment, and the overall size of the structure is the integer of all maximum alignments (including the alignment of the nested structure) times.

         As can be seen from the above figure and the two structure members, the maximum default alignment of S3 is 8, so the first member of s3 should be stored from the position of offset 8, other storage rules are the same as the above three rules, and finally S4 The size occupies 32 Bytes.

3. Why there is memory alignment

1. Platform reason (transplant reason):
Not all hardware platforms can access any data at any address; some hardware platforms can only fetch certain types of data at certain addresses, otherwise a hardware exception will be thrown.
2. Performance reasons:
data structures (especially stacks) should be aligned on natural boundaries as much as possible.
The reason is that to access unaligned memory, the processor needs to make two memory accesses; while aligned memory accesses require only one access.

Why does the processor need to make two memory accesses for unaligned memory? See below:

 

Under the 32-bit machine, 4 Bytes can be accessed at a time. Assuming that memory alignment is not used, there is a variable c of char type before the variable of type int, then the processor needs to access i completely, and it needs to access c for the first time. variable and the first three bytes of the i variable, and then access the last byte of i for the second time, so that it needs to be accessed twice to fully access i.

In general:

The memory alignment of the structure is the practice of exchanging space for time.

 When designing the structure, we must not only satisfy the alignment, but also save space. We should:

Let members who occupy a small space gather together as much as possible.

Guess you like

Origin blog.csdn.net/m0_74265792/article/details/130142720