iOS_Memory Alignment Principles

Memory Alignment Principles

1. Alignment rules for data members: For data members of a structure ( struct) or a union ( union), the first data member is placed offsetat a place where is 0, and the starting position of each data member in the future should be from the size of the member or the size of the member The sub-member size (as long as the member has sub-members, such as arrays, structures, etc.) starts from an integer multiple (for example, int4 bytes, it needs to be stored from an address that is an integer multiple of 4)
2. A structure as a member: if a If there are some structure members in the structure, the structure members should be stored from the integer multiple of the maximum internal element size (there is a structure in it, and there strcut ais struct ba bstructure in it char, intetc., then bit should be stored from an integer multiple of 8)
3. The total size of the structure, that is, sizeofthe result, must be an integer multiple of the largest member in it, and the deficiency needs to be filled

Object application memory size VS system allocated memory size

Person *p = [[Person alloc] init];

1. The object applies for the memory size . class_getInstanceSize([p class])When the object applies for memory, the system will perform memory optimization, binary rearrangement, and apply for memory according to the memory alignment principle (aligned with 8 bytes) . When the memory is opened, the memory will be opened according to the memory size requested by the object, and the memory will be aligned with 16 bytes.
malloc_size((__bridge const void *)(p))

おすすめ

転載: blog.csdn.net/FlyingKuiKui/article/details/120686522