For most of the programmers at the bottom of the study, the memory should be no stranger to it, today the young students to a wave of ugliness.
Definition of memory alignment
Memory alignment should be the "domain" of the compiler. The compiler places each "data unit" in place in the program. "Memory alignment" should be "transparent" to most programmers who have studied the underlying layer.Copy the code
Why memory alignment
Because I am not good at writing, I borrow the explanation from Baidu Encyclopedia.
- 1. Platform reasons (porting reasons) : Not all hardware platforms can access any data at any address; Some hardware platforms can only fetch certain types of data at certain addresses or throw hardware exceptions.
- 2. Performance reasons: Data structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that in order to access unaligned memory, the processor needs to make two memory accesses; Aligned memory access requires only one access.
Memory alignment rules
A: It’s long. To be honest, I’m too lazy to read it. Never mind, I’ll explain later)
- 1: Data member alignment rule: struct (or union) data member, no
A data member is stored at offset 0. The starting location of each data member is a multiples of the size of the member or its children (as long as the member has children, such as arrays, structures, etc.). Min (the current starting position mn) m=9, n=4, then 9, 10, 11, 12, we need to start with 12
- Struct as members: If a structure has some struct members, then the struct members are required from
(struct A contains struct B, char,int,double, etc.); (struct A contains struct B, char,int,double, etc.)
- The total sizeof the structure, the result of sizeof, must be the largest within it
An integer multiple of the members. Any deficiencies must be made up
Bytes occupied by each type
I know you all know about this chart, but it feels ritualistic not to put it on
Example of memory alignment
Rory above the wordy bar is finally over. Just look at the theory what harvest all have no feeling, or do not let the code verification explanation is better.
And I’m going to illustrate that with structures
- Case 1:
Note: {} stands for explanation. struct XGStruct1 { double a; // 8{bytes occupied} (0{start}-7{end}){bytes occupied} char b; / / 1 bytes {of} [8 {starting position} 1 bytes {of}] (8) position} {storage bytes int c; // 4{bytes bytes} [9{start} 4{bytes bytes}] 9 10 11 (12 13 14 15){bytes bytes} short d; // 2{1 {1}}struct1; // 2{2{1}}struct1; Memory alignment rule 1: The start position must be an integer multiple of the number of bytes used to start storage. (for example: char b, due to the starting position is the number of bytes integer times can save directly, int c, start from September, not 4 integer times, so start from 12) storage size is: {0} 17 18 because there are a double for the maximum number of bytes in the structure: 8 (memory alignment rule 3: The memory must be an integer multiple of the maximum number of bytes. So it has to be an integer multiple of 8. 18-->24) so the memory size is 24.Copy the code
NSLog(@"%lu",sizeof(struct1)); Log Output: 24Copy the code
- Example 2:
This should be more graphic.
struct XGStruct2 { int b; //4{bytes bytes} [0{start} 4{bytes}],(0 1 2 3){bytes bytes} char c; / / 1 bytes {of} [4 {starting position} 1 bytes {of}], (4) position} {storage bytes short d; / / 2 bytes {of} [5 {starting position} 2 bytes {of}], 5 (6, 7) location} {storage bytes short e; / / 2 bytes {of} {starting position} [8, no. 2 bytes {of}], (8, 9) {byte storage location}} struct2; 1, the size of memory is 10 (0-9) 2, the largest byte in memory is int (4), so it is an integer multiple of 4. NSLog(@"%lu",sizeof(struct2)); Output: 12Copy the code
Extensions to memory alignment rules
The above two examples should illustrate the rules for memory. I’m sure you all get the idea. So, let’s go to the next step: structure nested structure.
Continue with the above two examples and correct them:
struct XGStruct1 { double a; char b; int c; short d; }struct1; Struct XGStruct2 {int b; char c; short d; short e; }struct2; XGStruct3: struct XGStruct3 {int b; XGStruct3: struct XGStruct3 {int b; char c; short d; short e; struct XGStruct1 xgs; }struct3; So if you think about it for a second, how much memory does it take up?Copy the code
Explained:
In the first place. Let's go through the rules and make a rough guess at what it's going to look like. And then I'm going to test it. Struct XGStruct3 {int b; //4{bytes bytes} [0{start} 4{bytes}],(0 1 2 3){bytes bytes} char c; / / 1 bytes {of} [4 {starting position} 1 bytes {of}], (4) position} {storage bytes short d; / / 2 bytes {of} [5 {starting position} 2 bytes {of}], 5 (6, 7) location} {storage bytes short e; Struct xgstruct {double a; //2{double a} [2{double a}], (8 9){double a}; // [10 8] 10 11 12 13 14 15 (16 17 18 19 20 21 22 23) int b; // [24 4] (24 25 26 27) char c; // [28 1] (28) short d; // [29 2] 29 (30 31) }xgs; }struct3; NSLog(@"%lu",sizeof(struct3)); Output: 32 really !!!!!Copy the code
You u can verify this by writing more examples. It seems that the memory alignment rules work pretty well.