Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - IOS architecture memory allocation
IOS architecture memory allocation
CGPoint is a structure in OC, and the structure is generally memory aligned and allocated.

The offset of each member of the 1. structure relative to the first address of the structure is an integer multiple of the size of the member. If necessary, the compiler will add padding bytes between members.

2. The total size of the structure is an integer multiple of the size of the widest member of the structure.

3. The first address of a structure variable can be divisible by the size of its widest basic type member.

4. For a composite structure with structural variables in the structural member attribute, when determining the widest basic type member, the child members of the composite type member should be included. However, when determining the offset position of the members of the composite type, the composite type is regarded as a whole.

5. Summary: The size of the structure is equal to the offset of the last member plus its size plus the number of padding bytes at the end, that is, sizeof (struct) = offsetof (last term) +sizeof (last term) +sizeof (tail padding).

Example 1:

The offset of the 1 th member from the first address of the structure is 0, which is an integer multiple of the member int i (length 4).

The offset of the second member of the structure from the first address is 4, which is an integer multiple of the member char c (length 1). (Because the total size of the structure is an integer bit of the widest member size of the structure, if the structure only has these two members, then three padding bytes will be added after char c, but now there are three members, so padding is not needed. )

The offset of the third member relative to the first address of the structure is 5, which is not an integer multiple of the member int x, so three bytes are filled before x (or after c) to make the offset of x reach 8, which becomes an integer multiple of 4. So the memory size of this structure is 4+ 1+3+4.

Example 2: Each member type has the same number of members, but the order is different and the memory size is different.

Example 3: Composite Structure

#pragma pack(n) // The compiler aligns the button with n bytes and sets the widest member size of the structure (whichever is smaller than the actual widest member size). In other words, the final length of the structure is an integer multiple of n.

#pragma pack() // Cancel custom alignment.

#pragma pack(puch, 1) // Save the original alignment and set a new alignment.

#pragma pack(pop) // Restores the previously saved alignment state.

#pragma pack parameter should be "1", "2", "4", "8" or "16"

The expected #pragma pack parameter is 1, 2, 4, 8 or 16.

demonstrate

Set memory calculation after alignment

1. When the set alignment length is less than the current member length, the member offset is an integer multiple of the member length.

2. When the set alignment length is greater than the current member length and less than the longest member length, the member offset is an integer multiple of the set alignment length.

3. When the set alignment length is greater than the longest member length, the member offset will be aligned according to the actual size of the current member.

4. When the set alignment length is less than the actual longest member length, the length of the structure is an integer multiple of the set alignment length.

5. When the set alignment length is greater than or equal to the actual longest member length, the length of the structure is an integer multiple of the actual longest member length.