Read to understand the byte alignment analysis

# Write at the beginning, the compiler aligns value 8byte

  As stated, first of all talk about why you need byte alignment, this seems a waste of space, it would first look at the comparison of a written explanation:

    1. platform requirements, some CPU can only access the memory address is an even address, the time you put data on the odd-numbered address, the error will be

    2. Performance requirements, this has to do with the register, the number of times some of the data are not aligned, then need to use the register will increase  

   Say something popular, like a book, each page has a word, but not necessarily filled; write too full, no tricks, can not see.

 

  Here, first described three concepts:

    1. align themselves value: for example, short of 2byte, double of 8byte

    2. Specify alignment value: the compiler's own decision; or programmer decision

1  #pragma pack (n)
 2 ***
 3 ***
 4  #pragma pack ()

    Then the internal data specified alignment value 2, n is 3 lines

    3. Effective alignment values: the minimum values ​​of the above two

 

  Align the rules it:  

    1. The address data itself should be stored, the data itself, the effective value of an integral multiple alignment

    2. For the structure, since the rule 1, and there is an array of structures, the effective own internal data structure alignment is aligned valid maximum values.

 

  So it is quite abstract, we point example

1 struct A {
2    char a;
3    int b;   
4 };

First, a self-alignment values ​​1, developed 8 is aligned, the effective alignment values ​​min {1, 8} = 1

  0x0     0x1     0x2     0x3  
   a      

Subsequently, b need to go into his own when aligned valid address must be min {4, 8} = 4, thus 0x1, 0x2, 0x3 are not kept, prepared from 0x4 and storage

  0x0     0x1     0x2     0x3     0x4     0x5     0x6     0x7  
   a          b    b    b    b

As above, it is very simple, here to talk about the rule 2, to add structure A char c

1 struct A {
2    char a;
3    int b;
4    char c;     
5 };

Less interesting things happen, the effective alignment values ​​A 1, b is 4, c is 1, then A is 4, so the back unit 3 c are occupied, think, if not so filled, you think about it for a second element of the b array of structures of his address is not an integer multiple of 4.

 0x0   0x1   0x2   0x3   0x4   0x5   0x6   0x7   0x8   0x9   0xA   0xB 
  a         b   b   b   b   c       

 

Therefore, fundamental research, everything is in order to satisfy rule 1.

 

Then again, if the double type b into it?

1 struct A {
2    char a;
3    double b;
4    char c;     
5 };

A less effective alignment value 1, b is 8, C is 1, A is a 8, it should account for the overall 24byte

 0x0   0x1   0x2   0x3   0x4   0x5   0x6   0x7   0x8   0x9   0xA   0xB   0xC   0xD   0xE   0xF   0x10   0x11   0x12   0x13   0x14   0x15   0x16   0x17 
  a                 b   b   b   b   b   b   b   b   c              

Handle and a spout lip off, we have a few topics

 1 #pragma pack (4)
 2 struct S1 {
 3     char a;
 4     double b;
 5 };
 6 
 7 struct S2 {
 8     char a;
 9     S1 s;
10     char b;
11     char c;
12 };
13 
14 struct S3 {
15     int a;
16     char b[10];
17 };
18 #pragma pack ()

Note that the specified alignment value is changed to 4, take their own IDE try it!

Have questions, please contact [email protected]

 

Guess you like

Origin www.cnblogs.com/zhangzixu/p/11588300.html