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 inoffsetfor0In 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:intfor4 bytes, from4An integer multiple of the address is stored. If the current start storage location is9Need to be vacated9,10,11In the12The location can be stored
  • 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 aThe entitiesstruct b.bAre there inchar,int,doubleStudent: equal elements, thatbShould be from8Integer multiples of start storage
  • Finishing off: The total size of the structure, i.esizeofThe 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
  • aAccount for8 bytesAnd stored in the0 ~ 7location
  • bAccount for1 byteAnd stored in the8Position. because8is1Multiples of, satisfying the condition
  • cAccount for4 bytes.9 ~ 11Are not4Multiple of, cannot store, empty it out. socStored in the12 ~ 15location
  • dAccount for2Byte, stored in16 ~ 17location
  • Finally, the end work is carried out to meet the integer multiple of the largest internal member and complement to24
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
  • aAccount for8 bytesAnd stored in the0 ~ 7location
  • bAccount for4 bytesAnd stored in the8 ~ 11location
  • cAccount for1 byteAnd stored in the12location
  • dAccount for2Bytes,13not2Multiple of, cannot store, empty it out. sodStored in the14 ~ 15location
  • Finally, the end work is carried out to meet the integer multiple of the largest internal member and complement to16
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
  • aAccount for8 bytesAnd stored in the0 ~ 7location
  • bAccount for4 bytesAnd stored in the8 ~ 11location
  • cAccount for1 byteAnd stored in the12location
  • dAccount for2Bytes,13not2Multiple of, cannot store, empty it out. sodStored in the14 ~ 15location
  • eAccount for4 bytesAnd stored in the16 ~ 19location
  • strIs 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. sostrThe initial position of is24.strThe structure remains in the body after alignment24 bytes, soLGStruct3The size of24 plus 24 is 48
NSLog (@ "struct3: % lu", sizeof (struct3)); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- struct3:48Copy the code