This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

This is a learned to have no use, can’t learn also doesn’t matter knowledge point

Everybody big guy is really idle of panic listen to me break off break off pull ~~~ ha ~~

There is a memory allocation principle deep in the remote system, and it goes like this:

  1. Data member alignment rule: Struct (or union (union)) data members, no

A data member is placed at offset 0, and the starting location of each data member is to be stored

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.) (e.g., if int is 4 bytes, it starts with a multiple of 4

Store.

  1. Struct as members: If a structure has some struct members, the struct members are required from

(struct A contains struct B, struct b)

Char,int,double, etc., so b should be stored at multiples of 8.

  1. Wrap up: 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.


Suddenly a look, a tuo word, a bit Mongolian

Take a closer look…

Might as well take a look…

Do not panic, the ancients said: show your code…..

Let’s do an example

#import <malloc/malloc.h> #import <objc/runtime.h> struct TTStruct1 { char a; //1 byte -- occupies 0 bytes double b; //8 bytes -- take up 8 to 15 bytes int c; //4 bytes -- take up 16-19 bytes short d; //2 bytes -- 20-22 bytes} MyStruct1; Struct TTStruct2 {double b; //8 bytes -- 0 to 7 bytes int c; //4 bytes -- 8 to 11 bytes char a; //1 byte -- 12 bytes short d; } MyStruct2; Int main(int argc, const char * argv[]) {@autoreleasepool {NSLog(@"TTStruct1 size: %lu",sizeof(MyStruct1)); NSLog (@ "TTStruct2 size: % lu", sizeof (MyStruct2)); } return 0; }Copy the code

Here’s a picture:

You can see that two structures, the internal elements are the same, but the order is not the same, the final amount of memory is not the same, this is the ghost of the memory allocation principle

Is there anyone who can explain it against the principle?

That since everybody does not talk, I avoid for its difficult explanation, ha ha ~~~~~~

Let’s start with this structure:

struct TTStruct1 { char a; //1 byte -- occupies 0 bytes double b; //8 bytes -- take up 8 to 15 bytes int c; //4 bytes -- take up 16-19 bytes short d; //2 bytes -- 20-22 bytes} MyStruct1; // 22 bytes, but 22 is not a multiple of 8, +2, because 24 is a multiple of 8Copy the code

The first member is of type char, which takes up 1 byte, so it occupies the 0th byte

The second member is the type double, double type of 8 bytes, according to the principle of 1 starting position is to integer times the size of the current type start, so let’s start with 8 integer times, so while the first members only occupies the byte 0 and the second member is to store according to the rules from 8 bytes, so the second member account for 8 to 15 bytes

The third member is an int, and the int takes up 4 bytes, so it is stored from a multiple of 4. Since the second member takes up 8-15 bytes, and the next byte is the 16th byte, it is stored directly from 16 bytes, so the third member takes up 16-19 bytes

So the fourth one is 20-22 bytes, so the size of the structure is 22 bytes, but rule 3 states that the total size must be a multiple of the largest internal member, which is 8 bytes, so the total size + 2,24 is a multiple of 8.

So this structure ends up being 24

That’s the interpretation of rule number one and rule number three

Here’s the second rule:

If there is a structure in the structure, the start position of temp is stored from an integer multiple of the size of the largest member in the structure. If the largest member is 8 bytes, the start position of temp is an integer multiple of 8, so start from 16

At this point, the perfect interpretation of memory allocation principle ~~~

When I was in front of my small partner, a small partner who might want to tear down my table came up with a sentence: “Brother dei, isn’t it said that class is also a structure? How can I use the memory size of the class not match your principle?”

I can take it down my throat, okay?

On the way up, I said a word, which overwhelmed him. I said, “Don’t I know that classes are structures? Don’t I know the memory size mismatch principle for classes? You know why I didn’t say anything? Don’t you know there are enough words in this one? How can I participate in the Nuggets Challenge after I have written one? You want to know why don’t you get on to the next one? You’re still talking to me. Do you want me to buy you dinner? Ha ha ha ~ ~ ~”