preface

As we all know, every variable, constant, class, structure, and so on in a program is allocated its own memory space by the system, and the rules for allocating memory are different for different code. So today we’ll take a look at how the system deals with structure in iOS.

Before we look at structure memory allocation, let's take a look at the memory size of our common data types:Copy the code

First of all, let’s understand the rules of structure storage allocation through words:

1. Data member alignment rules: The data member of a struct (or union), 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). Structure, etc.) (for example, if int is 4 bytes, it is stored from the address that is a multiple of 4. Min (current starting position m n) m = 9 n = 4 9 10 11 12

2. Struct as members: If a struct has members, then the members are stored from an integer multiple of the size of the largest element in the struct.

3. Wrap up: the total sizeof a structure, the result of sizeof, must be an integer multiple of its largest internal member. What is lacking must be made up

A blog that is only explained by words and not verified by code is a rogue; Let’s take a look at the structure’s memory allocation through the code.

We have created two structures CYFStruct1 and CYFStruct2, which have four variables a, B, C, and D respectively. If we look carefully, the variables b and C are exactly the same except that they are in a different order in the structure, do struct1 and struct2 occupy the same amount of memory? Let’s calculate it together according to the above principles;

First, CYFStruct1, we can see that the four variables a, B, C, D account for the largest number of bytes is double, the number of bytes is 8, so starting with double, we allocate 8 bytes to A [0-7];

Followed by b for char types, char type in the number of bytes to 1, we shall, in accordance with the “after each data member to store the starting position of the members from the member or members of the size of the son size (as long as the son of the members, such as arrays, structures, etc.) the integer times of start” this principle, 8 to 1 integer times, So we assign b a byte [8];

Then we go to c, where c is an int, and the number of bytes of an int is 4. According to the integer multiple rule, 9,10,11 are not multiples of 4, so we should start with 12, so c has memory of (9,10,11,[12,13,14,15];

Finally, d is short, and the number of bytes of short is 2. According to the principle of integer multiples, the following 16 is an integer multiple of 2, so the memory of D is [16,17].

And finally we’re looking at the result of the total sizeof the structure, which is sizeof. Must be an integer multiple of its largest internal member. In CYFStruct1, the largest member is of type double and the number of bytes is 8, so the minimum integer multiple of 8 from 17 to 24.

So we can conclude that CYFStruct1 occupies 24 memory;

Since the struct CYFStruct1 and CYFStruct2, all the member variables are the same are they also the same amount of space? So let’s verify that

First, CYFStruct2, whose maximum member variable is a, is of type double, so we can assign it 8 bytes [0-7];

B is an int, and we can allocate 4 bytes to it.

Then c is a char, we can assign it 1 byte [12];

And then finally d is short, so we can’t divide d with 13 because it’s actually a multiple of an integer, so we need to start with 14 and we end up like this (13,[14-15]

And finally we’re looking at the result of the total sizeof the structure, which is sizeof. Must be an integer multiple of its largest internal member. The maximum member in CYFStruct2 is of type double, and the number of bytes is 8. Therefore, the following 16 meets the condition, and the memory of CYFStruct2 is 16 bytes.

Is not very surprised, is not very surprised, next we run the code to see!

As you can see from the figure above, the system does allocate 24 and 16 memory to these two structures.

So far, we have seen how simple structures are allocated, so how do we calculate if the remaining properties in a structure are a structure? The diagram below:

How much memory should be allocated to CYFStruct3? Let’s explore:

First of all, we can see that the largest attribute in CYFStruct3 is a double type, accounting for 8 bytes, so a should be allocated 0-7, 8 bytes;

Then b is an int, accounting for 4 bytes, according to the starting position of the integer b allocation should be [8-11]

And c is a char, one byte, so it has 12 memory;

Then d is short, accounting for 2 self. The following 13 does not meet the condition, so it should start from 14, so d’s memory is (13, [14-15];

Then e is an int, accounting for 4 bytes. The initial value is 16, satisfying the condition of integer multiples. Therefore, the memory of E is [16-17].

Finally, STR, which is a structure type, also follows the same three principles when calculating its memory allocation;

The maximum attribute a is of type double and is 8 bytes long. It starts at 18, but is not a multiple of 8. If it is greater than 18 and is a multiple of 8, it is 24.

In the same step we calculate the memory of d in STR as [40-41]

And then we look at rule number three, “the total sizeof the structure, which is the result of sizeof,”. Must be an integer multiple of its largest internal member. It can be concluded that 48 is the minimum multiple of 8 that is greater than 41, so the memory occupied by CYFStruct3 is 48.

Let’s verify this again with code:

According to the console print result, our calculation is correct