byte alignment analysis in C + + __c++

Source: Internet
Author: User
byte alignment analysis in C + +

1. Sample CodeLet's take a look at the results of this program.






struct A
{
int A;
Char b;
Short C;
};


struct B
{
Char A;
int b;
Short C;
};


#pragma packs (2)
struct C
{
Char A;
int b;
Short C;
};


#pragma packs (1)
struct D
{
int A;
Char b;
Short C;
};


int _tmain (int argc, _tchar* argv[])
{


cout << sizeof (A) << "<< sizeof B <<" "<< sizeof C <<" "<< sizeof D &L t;<endl;
return 0;
}


The results of the operation are as follows:
8 12 8 7
Theoretically, the size of the structure A and B should be the same, which is caused by byte alignment.


2. Why do you want to align bytesWhy, then? To put it simply: to improve access efficiency. Byte is the smallest unit of memory space allocation, in which we define variables that can be placed in any location. In fact, the CPUs of different architectures have regular access to certain types of variables, such as when some CPU accesses an int variable, it reads from an even address, and the int type occupies 4 bytes (Windows platform). 0x0000,0x0004,0x0008 ..... This allows you to read the value of an int type variable only once.  Conversely, you need to read two times, and then the high and low byte to get the value of the int type, this way, the efficiency of the access is certainly increased. Usually when writing a program, there is no need to consider these situations, the compilation will be for us to consider these situations, unless the special architecture of the CPU programming when the need to consider. Of course, users can also control the alignment manually.
3. Some rules for the compiler to align bytesI've explained some of the compiler's principles for byte processing from the following three articles. Of course, in addition to some special compilers in the processing of byte alignment is not the same, I have not encountered these situations, it is not explained.


A. For the alignment values of the data type itself, the different types are aligned in different bytes.
Type alignment value (bytes)
Char
1
Short
2
Int
4
Float
4
Double
4
B. The self-aligned byte values of the class and struct body. For the alignment of the struct type with the class object: aligns with the largest aligned bytes among members. For example, in struct A, the alignment byte of int A is 4, larger than Char,short, so A's alignment byte is 4
C. Specifies the alignment byte value. Refers to the alignment value specified using the macro #pragma pack (n)
D. Valid alignment byte values for classes, structs, and members.   Valid alignment value =min (the class/struct/member's own aligned byte value, specifies the aligned byte value). Valid alignment values determine how the data is stored, and the sizeof operator calculates the size of the member based on valid alignment values. In short, effective alignment requires that the value of the address that the data member holds can be evenly divisible by the valid alignment value, that is, the address value% is effectively aligned to the value =0





4. Combining the compiler analysis example

According to the above principle, the size of struct a is analyzed. The member memory allocations for a struct are analyzed in the order defined.
struct A
{
int A;
Char b;
Short C;
}
For the sake of simplicity, I assume that the starting address for struct a access is 0x0000 the parse step if no alignment value is specified:


Step 1: According to the second, first select the alignment value for the struct: Select the largest alignment value in the member, that is int a, and the alignment value is 4

Step 2: Again, according to the fourth principle, determine the valid alignment value: That is, no alignment value is manually specified, the default value is used: 4 (Windows 32 platform)


Step 3:int A's valid address value =min (4,4), (because of 0x0000%4=0), so that A's address is from 0x0000~0x0003


The valid alignment value =min (1,4) For step 4:char B, which, in turn, starts with 0x0004 (because of ox0004%1=0), assigns a byte, and the address segment is allocated: 0x0000~0x0004


Step 5:short C's Valid alignment value =min (2,4), in theory, the assigned address should be contiguous (from 0x0005~0x00006), but because of the alignment requirements, the required address segment offset is required, thus from 0x0006 (offset+ 1, because 0x0006%2=0) begins, allocates 2 bytes of address 0x0006~0x0007.



So far, the allocation of address segments is as follows: 0x0000~0x0007 so that the size of sizeof (A) is =0x0000~0x0007 a total of 8 byte sizes, and 8%4=0 guarantees struct A's address segment and 40%-odd times.


Next, the size of struct B is analyzed, assuming that the starting address of struct B is 0x0000, the analysis steps are as follows:


struct B
{
Char A;
int b;
Short C;
}


Step 1: True struct B Alignment value: Select the largest alignment value in the member, that is int a, and the alignment value is 4

Step 2: Determine to manually specify the alignment value, using the default value: 4 (Windows VC6.0 Platform)


Step 3:char A valid address value =min (1,4), the address of A is 0X0000 (because 0x0000%1=0)


The effective Alignment value =min (4,4) of step 4:int B, which is assigned 4 bytes at the beginning of 0x0004~0x0007 (because of ox0004%4=0), and the current J address segment assignment is: 0x0000~0x0007


Step 5:short C's Valid alignment value =min (2,4), which is offset by 2-byte addresses starting with 0x0008~0x0009 (because of 0x0008%2=0).

To stop, the allocation of the address segment is: 0x0000~0x0009 A total of 10 bytes, but struct B's alignment value is 4, which requires the address lot to be offset by 2 bytes, which is a total of 12 (because of 12%4=0) byte size from 0x0000~0x000b. In this way, sizeof (B) =12






To use pragma to manually change the byte alignment value, first look at the definition of struct C:




#pragma packs (2)
struct C
{
Char A;
int b;
Short C;
};


In code, the alignment value is specified as 2 bytes manually, and the parse steps are as follows:




Step 1: Determine the structure body C alignment value: Select the largest alignment value in the member, that is int a, the alignment value is 4

Step 2: Determine to manually specify the alignment value, using the manually specified value: 2


Step 3:char A valid address value =min (1,2), (because 0x0000%2=0), so the address of A is 0x0000


The valid alignment value =min (4,2) For step 4:int B, which is assigned 4 bytes at the beginning of 0x0002~0x0005 (because of ox0002%2=0), and the current address segment assignment is: 0x0000~0x0005


The effective Alignment value =min (2,2) for step 5:short C, which, since the justification is required, allocates 2-byte addresses from 0x0006 (because of 0x0006%2=0) 0x0006~0x0007



So far, the allocation of address segments is: 0x0000~0x0007 A total of 8 bytes, while also ensuring struct C alignment (2-byte alignment, pragma (2)), sizeof (c) =8


Note that this situation differs from the case of Struct B, where the sizeof size of B is 12 bytes and the sizeof size of C is 8 bytes.


The final analysis of the #pragma pack (1) is very simple, the alignment value is 1, because 1 can be divisible by any data, so struct D's member variable access order is continuous, so it is OK, sizeof (D) =sizeof (int) +sizeof ( Char) +sizeof (short) =4+1+2=7 (for example, from 0x0000~0x0006)


Summarize
Be careful when you think about byte alignment, to figure out several important concepts, such as the type itself alignment value, the manual alignment value, and the valid alignment value, the valid alignment value determines the last access mode, and the valid alignment value equals the smaller of the type's own alignment value and the manual alignment value. With this in mind, the operation of the sizeof operator on the type or structure is fully understood.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.