Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Principles of memory alignment
- Data member alignment rules: Structure (
struct
) or combined (union
), the first data member is placed inoffset
for0
In the future, the starting location of each data member is to start with an integer multiple of the size of the member or its children (as long as the member has children, such as arrays, structs, etc.) \- Such as:
int
for4 bytes
, from4
An integer multiple of the address is stored. If the current start storage location is9
Need to be vacated9
,10
,11
In the12
The location can be stored
- Such as:
- Struct as members: If a structure has some struct members, the struct members are stored from an integer multiple of their largest internal element size
- Such as:
struct a
The entitiesstruct b
.b
Are there inchar
,int
,double
Student: equal elements, thatb
Should be from8
Integer multiples of start storage
- Such as:
- Finishing off: The total size of the structure, i.e
sizeof
The result must be an integer multiple of the largest internal member, and the less must be made up
Case 1
struct LGStruct1 {
double a;
char b;
int c;
short d;
}struct1;
Copy the code
a
Account for8 bytes
And stored in the0 ~ 7
locationb
Account for1 byte
And stored in the8
Position. because8
is1
Multiples of, satisfying the conditionc
Account for4 bytes
.9 ~ 11
Are not4
Multiple of, cannot store, empty it out. soc
Stored in the12 ~ 15
locationd
Account for2
Byte, stored in16 ~ 17
location- Finally, the end work is carried out to meet the integer multiple of the largest internal member and complement to
24
NSLog (@ "struct1: % lu", sizeof (struct1)); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- struct1:24Copy the code
Case 2
struct LGStruct2 {
double a;
int b;
char c;
short d;
}struct2;
Copy the code
a
Account for8 bytes
And stored in the0 ~ 7
locationb
Account for4 bytes
And stored in the8 ~ 11
locationc
Account for1 byte
And stored in the12
locationd
Account for2
Bytes,13
not2
Multiple of, cannot store, empty it out. sod
Stored in the14 ~ 15
location- Finally, the end work is carried out to meet the integer multiple of the largest internal member and complement to
16
NSLog (@ "struct2: % lu", sizeof (struct2)); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- struct2:16Copy the code
Case 3
struct LGStruct3 {
double a;
int b;
char c;
short d;
int e;
struct LGStruct1 str;
}struct3;
Copy the code
a
Account for8 bytes
And stored in the0 ~ 7
locationb
Account for4 bytes
And stored in the8 ~ 11
locationc
Account for1 byte
And stored in the12
locationd
Account for2
Bytes,13
not2
Multiple of, cannot store, empty it out. sod
Stored in the14 ~ 15
locatione
Account for4 bytes
And stored in the16 ~ 19
locationstr
Is the structure type, and the maximum number of members8 bytes
. Contains structure members, stored from an integer multiple of the size of the largest element inside them. sostr
The initial position of is24
.str
The structure remains in the body after alignment24 bytes
, soLGStruct3
The size of24 plus 24 is 48
NSLog (@ "struct3: % lu", sizeof (struct3)); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- struct3:48Copy the code