Byte alignment analysis in C ++

Source: Internet
Author: User
Document directory
  • Read guide:
  • Sample Code
  • Why byte alignment
  • Rules of the compiler for byte alignment
  • Analysis example based on Compiler Principles
  • Summary
  • 1. Sample Code
  • 2. Why byte alignment?
  • 3. compiler rules for byte alignment
  • 4. Combined with compiler analysis examples
  • Summary
Read guide:
  1. Sample Code
  2. Why byte alignment
  3. Rules of the compiler for byte alignment
  4. Analysis example based on Compiler Principles
  5. Summary
1. Sample Code

Let's take a look at the running results of this program.

Sample Code

Struct
{
Int;
Char B;
Short c;
};

Struct B
{
Char;
Int B;
Short c;
};

# Pragma pack (2)
Struct C
{
Char;
Int B;
Short c;
};

# Pragma pack (1)
Struct D
{
Int;
Char B;
Short c;
};

Int _ tmain (int argc, _ TCHAR * argv [])
{

Cout <sizeof (A) <"" <sizeof B <"" <sizeof C <"<sizeof D <endl;
Return 0;
} The running result is as follows: 8 12 8 7

Theoretically, the size of struct A and struct B should be the same. This is caused by byte alignment.

2. Why byte alignment? Simply put: To improve access efficiency. Byte is the minimum unit for memory space allocation. in the program, the variables we define can be placed anywhere. In fact, CPUs of different architectures have regular access to specific types of variables. For example, some CPUs read from even addresses when accessing int variables, int type occupies 4 bytes (windows ). 0 X random, 0X0004, 0X0008... the value of the Int type variable can be read only once. On the contrary, you need to read twice, and then combine the high and low bytes to get the int type value. In this way, the access efficiency is certainly improved. When writing a program, you do not need to consider these situations. compilation will always consider these situations, unless you need to consider the CPU programming for those special architectures. You can also manually control alignment. 3. compiler rules for byte alignment

I will explain from the following three principles of the compiler's byte processing. Of course, except for some special compilers, the methods for processing byte alignment are also different. I have never encountered these situations and will not explain them.

A. The alignment values of data types are aligned in different bytes.
Type Alignment value (bytes)
Char 1
Short 2
Int 4
Float 4
Double 4
B. Alignment byte values of classes and struct. Alignment principle for struct types and class objects: Use the largest alignment byte among the members to alignment. For example, in Struct A, the alignment byte of int a is 4, which is larger than char and short, so the alignment byte of A is 4 c. Specify the alignment byte value. This indicates the alignment value specified by the macro # pragma pack (n ).

D. Effective alignment of classes, structures, and members. Valid alignment value = min (Class/struct/member's own alignment byte value, specify the alignment byte value ). The valid alignment value determines the data storage method. the sizeof operator calculates the member size based on the valid alignment value. To put it simply, effective alignment requires that the address values stored by data members be divisible by valid alignment values, that is, the address value % valid alignment value = 0

4. Combined with compiler analysis examples

According to the above principle, analyze the size of Struct. The memory allocation of struct members is analyzed in the defined order. Struct
{
Int;
Char B;
Short c;
} For the sake of simplicity, I assume that the starting address of access to Struct A is 0x0000. If no alignment value is specified, the analysis procedure is as follows: Step 1: first select the alignment value for the struct according to the second entry: select the largest alignment value in the member, that is, int a, and the alignment value is 4

Step 2: Determine the valid alignment value based on the fourth principle. If no alignment value is manually specified, use the default value: 4 (windows 32 platform)

Step 3: The valid address value of int a is min (0000%) (because 0x4 = 0). In this way, the address of a is from 0X0000 ~ Zero x 0003

Step 4: assign a byte starting from 0x0004 (because Ox0004 % 1 = 0) for the valid alignment value of char B = min, address segment allocation: 0x0000 ~ Zero x 0004

Step 5: short c's valid alignment value = min (2, 4). Theoretically, the allocated address should be continuous (from 0x0005 ~ 0x00006), but because the alignment is required, the requested address segment Offset starts from 0x0006 (Offset + 1, because 0 x 0006% 2 = 0, allocate 2 bytes of address 0x0006 ~ 0x0007.


So far, CIDR Block allocation is: 0x0000 ~ Size of sizeof (A) Like 0x0007 = 0x0000 ~ 0x0007 is A total of 8 bytes. At the same time, 8% 4 = 0 ensures that the address segment of Struct A is an even multiple of 4.

Next, we analyze the size of Struct B, and assume that the starting address of Struct B is 0x0000. The analysis procedure is as follows:

Struct B
{
Char;
Int B;
Short c;
} Step 1: True structure B alignment value: select the maximum alignment value of the member, that is, int a, and the alignment value is 4.

Step 2: confirm to manually specify the alignment value. Use the default value: 4 (windows 32, VC6.0 platform)

Step 3: The valid address value of char a is min (0000), and the address of a is 0 X 0000% (because 0x1 = 0)

Step 4: Valid alignment value of int B = min (). The address ranges from 0x0004 ~ 0x0007 (because Ox0004 % 1 = 0), 4 bytes are allocated. The current j address segment distribution is: 0x0000 ~ Zero x 0007

Step 5: Valid alignment value of short c = min (2, 4), c from 0x0008 ~ 0x0009 (since 0 x 0008% 2 = 0), offset the address 0x0006 ~ of 2 bytes ~ 0x0007.

End to end, the address segment is allocated: 0x0000 ~ 0x0009 has 10 bytes in total, but the alignment value of Struct B is 4. This requires that the address location be shifted to 2 bytes, which is from 0x0000 ~ 0x000B Total 12 (because 12% 4 = 0) bytes. In this way, sizeof (B) = 12


Then use Pragma to manually change the byte alignment value. Let's take a look at the definition of Struct C:

# Pragma pack (2)
Struct C
{
Char;
Int B;
Short c;
};

In the code, the alignment value is manually specified to 2 bytes. The analysis steps are as follows:

Step 1: Determine the alignment value of the structure C: select the maximum alignment value of the member, that is, int a. The alignment value is 4.

Step 2: confirm to manually specify the alignment value, and use the manually specified value: 2

Step 3: The valid address value of char a is min (0000%) (because 0x0000 2 = 0), so the address of a is 0 x

Step 4: Valid alignment value of int B = min (). The address ranges from 0x0002 ~ 0x0005 (because Ox0002 % 2 = 0), 4 bytes are allocated. The current address segment distribution is: 0x0000 ~ Zero x 0005

Step 5: The valid alignment value of short c is min (0006). Since alignment is required, it starts from 0 x 0006% (because 0x2 = 0, allocate 2 bytes of address 0x0006 ~ Zero x 0007


So far, CIDR Block allocation is: 0x0000 ~ 0x0007 consists of 8 bytes, and ensures the alignment of Struct C (2 bytes alignment, pragma (2), sizeof (C) = 8

Note that this is different from Struct B. The size of sizeof of B is 12 bytes, and that of C is 8 bytes.

Final analysis # pragma pack (1) this situation is very simple, alignment value is 1, because 1 can be divisible by any data, therefore, the access sequence of the Struct D member variables is continuous, so it is easy to do, sizeof (D) = sizeof (int) + sizeof (char) + sizeof (short) = 4 + 1 + 2 = 7 (for example, from 0x0000 ~ 0x0006)

Summary

When considering the byte alignment, you should be careful and clarify several important concepts, such as the type alignment value, manual alignment value, and valid alignment value. The valid alignment value determines the final access method, the valid alignment value is equal to the smaller alignment value of the type and that of the manual alignment value. With this understanding, we fully understand the sizeof operator's operation on types or all structures.

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.