IOS low-level exploration – memory alignment

The size of memory occupied by different data types

Memory alignment principles

  • 1: Alignment rules for data members: Struct (or union) data members, the first data member is placed at offset 0, and the starting position of each data member is from the size of the member or the size of the member’s children (as long as the member has children, such as arrays, structures, etc.)Integer timesStart (for example, if int is 4 bytes, it starts with a multiple of 4. Min (current starting position m n) m=9 n=4 9 10 11 12
  • 2: Struct as members: If a structure has some struct members, then the struct members are stored from an integer multiple of the size of the largest element in the structure.
  • The total sizeof the structure, the result of sizeof, must be an integer multiple of its largest internal member. What is lacking must be made up.

For example:

struct LGStruct1 {
    double a;       / / 8 7 [0]
    char b;         / / 1 [8]
    int c;          // 4 (9 10 11 [12 13 14 15]
    short d;        // 2 [16 17] 24 17+7=24
}struct1;

struct LGStruct2 {
    double a;       / / 8 7 [0]
    int b;          // 4 [8 9 10 11]
    char c;         / / 1 [12]
    short d;        // 2 (13 [14 15] 16
}struct2;

struct LGStruct3 {
    double a;       / / 8 7 [0]
    int b;          // 4 [8 9 10 11]
    char c;         / / 1 [12]
    short d;        // 2 (13 [14 15]
    int e;          // 4 [16 17 18 19] 24
    struct LGStruct1 str; 
    // (20 21 22 23 [24...47] 48 +LGStruct1(17) = 41-7=48 or [24+24] 48
}struct3;


struct LGStruct4 {
    double a;       / / 8 7 [0]
    int b;          // 4 [8 9 10 11]
    char c;         / / 1 [12]
    short d;        // 2 (13 [14 15]
    int e;          // 4 [16 17 18 19] 24
    struct LGStruct2 str; 
    // (20 21 22 23 [24...39] 40 +LGStruct2(16) = 40
}struct4;

Copy the code

A print

NSLog(@"%lu-%lu",sizeof(struct1),sizeof(struct2));
NSLog(@"%lu",sizeof(struct3));
NSLog(@"%lu",sizeof(struct4));

24-16
48
40
Copy the code

Note: Sizeof actually takes the amount of memory that the data occupies, in bytes.

Analysis of the

Char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char B: char (8 is a multiple of 1, so B is 1 bit at 8, int C is 4 bytes. Same as above, 9 is not a multiple of 4, 10, 11 is not a multiple of 4, So c is 4 bits in position 12 [12 13 14 15] short D is 2 bytes as above 16 is an integer multiple of 2, so D is 2 bits in position 16 [16 17] There is no structure as a member to wrap up the structure's total size (sizeof)(must be an integer multiple of its largest internal member. Struct2 double a = 8, a = [0 7] int b = 4 Char C takes 1 byte, placeholder [12] short D takes 2 bytes, placeholder (13 [14 15] No structure as member closure work The total size (sizeof) of a structure (must be an integer multiple of its internal largest member. Struct3 double a (8, 9, 10, 11) int b (8, 9, 10, 11) struct3 double a (8, 9, 10, 11) [12] short D (13 [14 15] int e (4 bytes) [16 17 18 19] struct LGStruct1 STR 24 bytes starting with an integer multiple of the size of the largest internal element (20 21 22 23 [24...47] Struct 1 (double,char,int,short); struct 3 (double,char,int,short); The total sizeof the closing work structure (sizeof) must be an integer multiple of its largest internal member. Struct4 double a = 8, a = [0 7] int B = 4, a = [0 7] [12] short D (13 [14 15] int e (4 bytes) [16 17 18 19] struct LGStruct2 STR takes 16 bytes starting with an integer multiple of the size of its largest internal element 8, i.e. 24 (20 21 22 23 [24...39]) Struct 2 (double,char,int,short); struct 4 (double,char,int,short); The total sizeof the closing work structure (sizeof) must be an integer multiple of its largest internal member. 39 is not an integer multiple of 8 bytes of the largest member, so the total size of the structure is 40Copy the code

To be continued