C ++ alignment

Source: Internet
Author: User

What is alignment and why:
In modern computers, memory space is divided by byte. Theoretically, it seems that access to any type of variables can start from any address, however, the actual situation is that access to specific variables is often performed at specific memory addresses, which requires various types of data to be arranged in space according to certain rules, instead of sequential emissions, this is alignment.
Alignment functions and causes: the processing of storage space varies greatly by hardware platform. Some platforms can only access certain types of data from some specific addresses. This may not be the case for other platforms, but the most common problem is that alignment of data storage according to the requirements of their platforms will cause a loss of access efficiency. For example, some platforms start from the even address each time they read data. If an int type (assuming a 32-bit System) is stored at the beginning of the even address, a read cycle can be read, if the data is stored at the beginning of the odd address, two read cycles may be required, and the high and low bytes of the result read twice can be pieced together to obtain the int data. Obviously, reading efficiency is greatly reduced. This is also a game of space and time.
Alignment implementation
Usually, we do not need to consider alignment when writing a program. The compiler will select the alignment policy of the target platform for us. Of course, we can also notify the compiler to pass the pre-compilation command to change the Alignment Method for the specified data.
However, because we generally do not need to care about this issue, the editor is aligned with the data storage. If we do not know it, we are often confused about some problems. The most common result is the sizeof result of the struct data structure, which is unexpected. Therefore, we need to understand Alignment Algorithms.
Alignment Algorithm:
The GCC version 3.2.2 Compiler (32-bit x86 Platform) is used as an example, to discuss how the compiler alignment each member in the struct data structure.
The struct is defined as follows:
Struct
{
Int;
Char B;
Short C;
};
Struct a contains a four-byte int, a one-byte char, and a two-byte short data. Therefore, the space used by a is 7 bytes. However, for the compiler, data members must be aligned in space. Therefore, the sizeof (strcut a) value is 8.
Now adjust the sequence of the member variables for this struct.
Struct B
{
Char B;
Int;
Short C;
};
At this time, it is also a total of 7 bytes of variables, but the value of sizeof (struct B) is 12.
The following uses the pre-compiled command # progma pack (value) to tell the compiler to replace the default value with the specified alignment value.
# Progma pack (2)/* specify to align by 2 bytes */
Struct C
{
Char B;
Int;
Short C;
};
# Progma pack ()/* cancel the specified alignment and restore the default alignment */
The value of sizeof (struct C) is 8.
Modify the alignment value to 1:
# Progma pack (1)/* specify to align by 1 byte */
Struct d
{
Char B;
Int;
Short C;
};
# Progma pack ()/* cancel the specified alignment and restore the default alignment */
The sizeof (struct d) value is 7.
For char data, its own alignment value is 1, for short data is 2, for int, float, double type, its own alignment value is 4, in bytes.
Calculation rules:
First consider completing (use N to calculate completing), then put a Variable N, and then consider completing (use M to calculate completing), continue to consider the next variable M. After all the variables are placed, consider structure alignment.
There are four conceptual values:
1. Alignment of the Data Type itself: Alignment of the basic data type described above.
2. Specify the alignment value: # The alignment value specified for progma pack (value.
3. The alignment value of a struct or class: The value with the largest alignment value among its members.
4. Valid alignment values of data members, struct, and classes: the alignment value of the data itself and the value smaller than the specified alignment value.
With these values, we can easily discuss the data structure members and their alignment. The valid alignment value n is the final value used to determine the data storage address. If the correct value is N, it indicates "alignment on N". That is to say, the "initial storage address % n = 0" of the data ". data variables in the data structure are discharged in the defined order. The starting address of the first data variable is the starting address of the data structure. The member variables of the struct must be aligned and discharged, and the struct itself must be rounded according to its own valid alignment values (that is, the total length occupied by the member variables of the struct must be an integer multiple of the effective homogeneous values of the struct, ). In this way, you cannot understand the values of the above examples.
Example Analysis:
Analysis example B;
Struct B
{
Char B;
Int;
Short C;
};
Assume that B is discharged from the address space 0x0000. The alignment value is not defined in this example. In the author's environment, this value is 4 by default. The alignment value of the first member variable B is 1, which is smaller than the alignment value 4 specified by default. Therefore, the valid alignment value is 1, therefore, the storage address 0x0000 is 0 x 0000% 1 = 0. the alignment value of the second member variable A is 4, so the valid alignment value is 4. Therefore, it can only be stored in the four consecutive bytes from the starting address 0x0004 to 0x0007, review 0 x 0004% 4 = 0, which is close to the first variable. The third variable C has its own alignment value of 2, so the valid alignment value is also 2, which can be stored in the two bytes from 0x0008 to 0x0009, Which is 0 x 0008% 2 = 0. Therefore, B content is stored from 0x0000 to 0x0009. Then, let's look at the alignment value of Data Structure B as the maximum alignment value in its variable (here it is B). Therefore, it is 4, and the valid alignment value of the struct is also 4. According to the requirements of the structure, 0x0009 to 0x0000 = 10 bytes, (10 + 2) % 4 = 0. Therefore, 0x0000a to 0x000b is also occupied by struct B. Therefore, B has 12 bytes from 0x0000 to 0x000b, and sizeof (struct B) = 12;
Similarly, analyze the above example C:
# Progma pack (2)/* specify to align by 2 bytes */
Struct C
{
Char B;
Int;
Short C;
};
# Progma pack ()/* cancel the specified alignment and restore the default alignment */
The first variable B's own alignment value is 1 and the specified alignment value is 2. Therefore, the valid alignment value of B is 1. Suppose C starts from 0x0000, then B is stored in 0x0000, conforms to 0 x 0000% 1 = 0; the second variable, its own alignment value is 4, and the specified alignment value is 2, so the valid alignment value is 2, therefore, the sequence is stored in four consecutive bytes, namely 0x0002, 0x0003, 0x0004, and 0 x 0002%. The alignment value of the third variable C is 2, so the valid alignment value is 2, which is stored in the order of 0x0006 and 0x0007, Which is 0 x 0006% 2 = 0. Therefore, from 0x0000 to 0x00007, a total of eight characters are stored in the C variable. And C's own alignment value is 4, so the valid alignment value of C is 2. Again 8% 2 = 0, C only occupies eight bytes from 0x0000 to 0x0007. So sizeof (struct c) = 8.
When the nested struct In the struct, each structure is considered as another structure for understanding. For example
# Pragma pack (4)
Struct
{
Double m_d;
Char M_a;
}
Struct B
{
A;
Char m_ B;
}
When calculating the alignment of struct B, calculate the maximum alignment values of elements a and m_ B, and the alignment of A is determined by the maximum values in m_d and M_a. When calculating the size of struct B, sizeof (A) = 12; the alignment value of struct B is 8, and the minimum value of the Set pack (4) is 4, so sizeof (B) = 16. if different headers have different alignment modes, the size of a single struct is different. In the above example, struct a is in a pack (8) header file, while struct B is in a pack (4) header file, sizof (A) = 16; and sizeof (B) = 20.

Related Article

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.