Object and pointer address assignment
main() {
Person *p = [Person alloc];
}
P is the pointer that creates the object, and the address of the pointer is automatically allocated memory address in the stack, as long as the variables in the method are allocated on the stack. And the address that P points to is the object.
Structure-memory alignment principles
1: Alignment rules for data members: Struct (or union) data members, the first data member is placed at offset 0, then the starting position of each data member is stored from the size of the member or the size of the member’s children (as long as the member has child members, 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 mn) 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.
3: finishing work: the total sizeof the structure, the result of sizeof, must be an integer multiple of its internal largest curriculum development :Cooci teacher members. What is lacking must be made up.
The byte corresponding to the data type
Bool /char:1 short:2 int/float/long:4 NSINTEGER/CGFloat/pointer/NSString /double/long Long: 8 bits
Struct RStruct {int a; //4 char b; //1 short c; //2 }Rstruct; It is directly calculated this time: A – [0-3], C – [4], and C – [6-7]. The size of its internal demand is 7, but its internal member variable is at most 4, so the overall demand is a multiple of 4, namely, 8
Summary: The structure’s memory-aligned cities are arranged according to the attributes.
Optimization of object-memory alignment
As you can see from above, the memory alignment of the structure is sorted by attributes, while the memory alignment of the object is not. Instead, the memory alignment of the compiler heap object is optimized. Object attributes are stored aligned to the maximum value multiple of the attribute (typically 8), while 16-byte alignment is aligned to the entire object. Because the system opened up memory according to the size of the attribute allocation, may lead to memory overflow.
Isa pointer structure
In the arm64 architecture, Pointers occupy 8 bytes /byte in memory, i.e., 64 bits /bit(1byte=8bit), which stores the following contents:
Bit-field declaration:
- NOPOINTER_ISA: indicates whether pointer optimization is enabled, whether it ISA pure ISA pointer, or whether it ISA non-pointer NOPOINTER_ISA pointer. 0 indicates a pure ISA pointer, and 1 indicates that it is not only a pointer to a class object, but also contains class information, object reference count, etc. \
- Has_assoc: indicates whether there is an associated object \
- Has_cxx_dtor: indicates whether the object is used by C++, and whether the object is managed by ARC in the ARC environment
- Shiftcls: Pointer address of the class object that marks the current object \
- Magic: space used by the debugger to determine whether the current object is a real object or not initialized
- Weakly_referenced: Indicates whether an object has a weak reference pointer \
- Deallocating: Marks whether the object is doing a dealloc operation \
- Has_sidetable_rc: flags whether there is a sitetable structure for storing reference count \
- Extra_rc: Marks the reference count of an object (stored first in this field and, when it reaches the upper limit, in the corresponding reference count table). For example, if the object’s reference count is 10, the extra_rc is 9. If the reference count is greater than 10, you need to use has_sideTABLE_rc above.
Pointers are designed to optimize memory for classes, save space, and avoid waste, so add additional information about the object in the free space
Isa and memory binding
The pointer is of type ISA_T, // uintptr_t bit // uintptr_t bit // struct of 8 bytes containing isa_bitfield}\
The isa_bitfield bitfield, which corresponds to different values for different system architectures