5 minutes for easy memory byte alignment

  • 2020-04-02 01:34:29
  • OfStack

Write out a struct and then sizeof, do you often wonder what the result is? Sizeof results tend to be larger than the total length of the variables you declare. What's going on here? Talk about byte alignment.

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * line

If the architecture is not aligned, the members of A will be stored side by side, resulting in sizeof(A) being 11. Obviously alignment wastes more space. So why use alignment?

Alignment and disalignment of the architecture is a tradeoff in time and space. Alignment saves time. Assuming that an architecture has a word length of w, it also assumes that data of width w is processed most frequently and most importantly on this architecture. It is also designed in order to improve the efficiency of the operation of w bit data. Like reading and writing... Half a million words are omitted

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /

There are a lot of reasons why you should align. We don't have much time to discuss why you should align. To get to the topic, how to judge the memory alignment rule, and how to get the result of sizeof, please keep in mind the following three principles :(in the case of no pragma pack macros, be sure to read the last line).

1: data member alignment rules: Structure (struct) (or joint (union) data members, members of the first data in the offset to zero, after each data member to store the starting position of the members from the member or members of the size of the son size (as long as the son of the members, such as arrays, structures, etc.) of integer times to start (such as int on a 32-bit machine is 4 bytes, will start from 4 integer times address storage.

2: structure as a member: If there are some struct members in a structure, the struct members should be stored from the address of the integer multiple of the maximum element size.

3: finishing touches: The total sizeof the structure, which is the result of sizeof, must be an integer multiple of its largest internal member.

By the time you finish reading these 3 principles,2 minutes has passed, seize the time, practice for 3 minutes:


typedef struct bb
{
 int id;             //[0]....[3]
 double weight;      //[8]... The principle of [15] 1
 float height;      //[16].. [19], the total length should be an integer multiple of 8. 3 [23] principle
}BB;
typedef struct aa
{
 char name[2];     //[0],[1]
 int  id;         //[4]... The principle of [7] 1
 double score;     //[8]....[15]    
 short grade;    //[16],[17]        
 BB b;             //[24]... [47] principle 2
}AA;
int main()
{
  AA a;
  cout<<sizeof(a)<<" "<<sizeof(BB)<<endl;
  return 0;
}

As a result,

48 24
Ok, the above all see clear, memory alignment basic clearance.

Let's talk about #pragma pack().

Put a #pragma pack(1) in front of the code and you'll be happy to see that the output is

32 16
Bb is 4+8+4=16, and aa is 2+4+8+2+16=32;

That's right,#pragma pack(1) tells the compiler that all alignment is an integer multiple of 1, in other words, there are no alignment rules.

See?

What is the result of pragma pack of 2? Sorry, five minutes is up. Test yourself.

Ps :Vc,Vs and so on compiler default is #pragma pack(8), so test our rule will be normal; Note that GCC defaults to #pragma pack(4), and GCC only supports 1,2, and 4 alignment. The alignment value calculated in the three rules cannot be greater than the n value specified in #pragma pack.


Related articles: