One: Memory alignment principle

Here are the rules for memory alignment and the bytes used by the various data types

1: Data member alignment rule: The first data member of a data member is placed at offset 0, and the starting position of each data member is stored from an integer multiple of the size of the member or its child members
2: Structure as member: If a structure has some structure members, then the structure members are stored from the address of the largest element in the structure, such as struct A contains struct B, char,int,double, etc., then b should be stored from the largest element (doule 8 bytes)
3: the total sizeof the structure, the result of sizeof, must be an integer multiple of its largest internal member

The following figure shows the size of bytes for each data type

Knowing the above principles, we began to calculate the internal storage size of the following structures according to this rule

From the picture above you can see, why struct1 agree with variables of struct2, has come out two different results? This will refer to the memory alignment rules to analyze

  • Struct1: A is of type double, occupying 8 bytes, so the storage address is 0-7; B is of type char, occupying 1 byte, occupying 8 bytes; C is of type int, occupying 4 bytes. Following the rule that the starting location of each data member should be stored from an integer multiple of the size of the member or its children, positions 9, 10 and 11 are not multiples of 4. Therefore, only positions 12, 13, 14 and 15 can exist. D is of type short, occupies two bytes, and has bits 16 and 17. According to the third rule, the total sizeof a structure, the result of sizeof, must be a multiple of the largest member in the structure. The result 17 does not satisfy this rule, so the result is 8 (double is 8 bytes, which is the largest member) * 3 = 24 bytes.

  • Struct2, a is of type double and stored in 0-7, B is of type int and stored in 8-11, C is of type char and stored in 12, d is of type short and stored in 14-15. Finally, according to the third rule, the result is 16 bytes

  • From the above analysis, it can be seen that the order of member variables is different, so we can arrange some attributes in the most reasonable way to save memory space.

Copy the code
struct LGStruct3 { double a; //8 [0 7] int b; //4 [8 11] char c; //1 [12] short d; //2 13 [14 15] // According to the first rule, the memory of the member variable after the first is stored according to the integer multiple of the size of the member or its child, so 13 is not a multiple of 2, the memory of the member variable is stored from 14; //4 [16 17 18 19] struct LGStruct1 str; //24 [24 + 24] 8 According to the second rule, if a structure has structure members, // is stored at integer multiples of the largest element, 24 being a multiple of 8. } struct1 = 24, struct = 24, struct = 24, struct = 48; ,,,,Copy the code

Let’s print it out and see if it’s correct

NSLog(@"struct1: %lu-struct2: %lu-struct3: %lu",sizeof(struct1),sizeof(struct2),sizeof(struct3)); ,,,,Copy the code

I want to add a little bit of sizeof here

  • 1:sizeof is an operator, not a function
  • 2:sizeof() the main object passed in is a data type, and the sizeof the object is returned at compile time