RT
1. What is memory alignment
Memory alignment means that for a variable of x byte size, its memory address must be an integer multiple of ** x
The memory alignment rules for structures are not described here
2. Why memory alignment
Memory alignment is an efficient way for the CPU to access data in memory
The CPU must follow certain rules when accessing the memory
- For 64-bit systems, the CPU can pull 8 bytes of aligned memory out of memory at a time
Such as [0], [16-23], [39] 32 -…
Memory alignment makes it more efficient for the CPU to access data
Intel’s x86 has no memory alignment requirement, but memory alignment can improve efficiency
The ARM architecture requires alignment or crash
3. How does memory alignment improve efficiency?
We use a diagram to simulate the actual CPU access to variables in the structure
Suppose I have three variables in a structure,Long (8byte), char (1byte), int (4byte)Now the CPU accesseschar(Green blocks in the picture)
We count the number of times the CPU needs to access the structure starting at address 0
- Without alignment in memory, CPU need access to five: [0] – > [8-15] – > [7-11] – > [7-9] – > [7-8].
- In the case of memory alignment, the CPU only needs to be accessed three times: [0-7]->[8-15]->[7-11].
Analysis of the
- Because memory alignment is required, int variables cannot start at byte 9 (address 8), but must start at the next 4byte aligned position, byte 12 (address 11).
- The memory 4byte alignment of int leaves char with 3byte padding, which has its own 4-byte area of alignment
- With this exclusive moment, the CPU reads the 8-byte alignment twice and then reads the char just once by changing the read width from 8byte->4byte
The size of the structure’s nested memory
The structure has one byte-alignment rule:
- The memory size of a structure must be an integer multiple of its internal largest variable byte
For example, in the previous example, 8+4+1=15, but the structure actually takes 16 bytes, i.e. [0-15]
So how do you calculate memory when you’re nesting?
- Let’s say we have S1, which has S2 nested inside it
- By whom is the “maximum attribute” of S1 determined when nested? S2? What is the maximum attribute of S1? What is the maximum property of S2?
- What if I have an *S2 nested?
There’s a lot of speculation, so just go ahead and see which one is right
-
- Maximum attribute: 8byte(belonging to S2)
typedef struct { double a; // Maximum attribute :8byte int b; char c; short d; } S2; //sizeof(S2) = 16 typedef struct { char a; S2 s; } S1; //sizeof(S1) = 24Copy the code
-
- Maximum attribute: 8byte (belonging to S1)
typedef struct { char c; } S2; //sizeof(S2) = 1 typedef struct { long a; // Maximum attribute :8byte S2 s; } S1; //sizeof(S1) = 16Copy the code
-
- Maximum attribute: 4byte (belonging to S2)
typedef struct { int b; // Maximum attribute :4byte char c; short d; } S2; //sizeof(S2) = 8 typedef struct { char a; S2 s; } S1; //sizeof(S1) = 12Copy the code
-
- Maximum property: pointer to S2
typedef struct { char c; short d; } S2; //sizeof(S2) = 4 typedef struct { char a; S2 *s; // Maximum attribute :8byte} S1; //sizeof(S1) = 16Copy the code
-
- Multilayer dolls: or in accordance with all the demolition into the basic unit after the largest attribute
typedef struct {
char c;
} S3;
//sizeof(S3) = 1
typedef struct {
long l;
char c;
short d;
S3 s;
} S2;
//sizeof(S2) = 16
typedef struct {
char a;
S2 s;
} S1;
//sizeof(S1) = 24
Copy the code
Summary:
-
- Byte alignment of structures: Aligns structures with multiples of the largest internal variables
-
- Internally nested structures cannot be viewed as a whole, but as properties
-
- The pointer is an 8byte variable
No matter how many layers of dolls, are all divided into the smallest attribute unit, and then to find the largest attribute, according to this size arrangement of the outermost structure size
If the nesting doll n layer (Sn-> SN-1 -> SN-2 ->… S2->S1,n>=2), and the outermost layer is 0. The maximum attribute is at layer K (k<=n). For the I layer structure
- I >k, the size of the structure of layer I finds the maximum attribute within the interval of layer [I, n]
- I <=k, the structure size of the i-layer is to find the maximum attribute of the k-layer interval
- The same is true for treenesting dolls
A pit
Take a chestnut
typedef struct { int b; // Max attribute :4byte int a; char c; short d; } S2; //sizeof(S2) = 12 typedef struct { char a; S2 s2; short b; } S1; //sizeof(S1) = 20Copy the code
S1 is declared in the following order:
-
- char a
-
- S2 s
-
- short b
A total of 20 byte S1
If you change the declaration order of S1:
-
- S2 s
-
- char a
-
- short b
typedef struct { int b; // Max attribute :4byte int a; char c; short d; } S2; //sizeof(S2) = 12 typedef struct { S2 s2; char a; short b; } S1; //sizeof(S1) = 16Copy the code
Only 16 byte S1
guess
The memory address of a nested structure must be an integer multiple of its internal maximum variable size
- Char – S2 – short arrangement
S2 as a whole must start at a multiple of int (4byte), leaving a 3byte padding
char[0]
padding[1 3]
S2[4 15]
short[16 17]
padding[18 19]
- S2 – char – short arrangement
S2[0 11]
char[12]
short[13 14]
padding[15]
conclusion
From this example we can see that the structure has different memory alignment rules inside the parent structure than normal variables.
type | variable | The structure of the body |
---|---|---|
The length of the | x | x |
Maximum variable length | oneself | y |
Memory alignment principles | X-byte alignment | Y byte alignment |
And that makes sense
- Variables are unsplit as a whole, and the minimum granularity of memory alignment is X
- The structure length x is an integer multiple of the maximum variable length y, and the minimum granularity of memory alignment is Y