C ++ byte alignment

Source: Internet
Author: User

View the sizeof problem of unionCPUPeer

Consider the following: (default alignment)

Union u
{
Double;
Int B;
};

Union
U2
{
Char A [13];
Int B;
};

Union U3
{
Char A [13];
Char B;
};

Cout <sizeof (u) <Endl; // 8
Cout <sizeof (U2) <Endl; // 16
Cout <sizeof (U3) <Endl; // 13

We all know that the size of Union depends on the size of one member that occupies the largest space among all its members. So for u, the size is the largest double type member A, so sizeof (u) = sizeof (double) = 8. However, for U2 and U3, the maximum space is an array of char [13] type. Why is the size of U3 13 and that of U2 16? The key lies in the member int B in u2. Because of the existence of int type members, the U2 alignment is changed to 4. That is to say, the U2 size must be 4 to the world, therefore, the occupied space is changed to 16 (the nearest 13 peer ).

Conclusion:The alignment of composite data types, such as Union, struct, and class, is the alignment of members with the largest alignment among members.

By the way, the 32 C ++ uses eight-bit bounds to increase the running speed. Therefore, the compiler tries its best to put the data in the world to improve the memory hit rate. The field can be changed. The # pragma pack (x) macro can be used to change the method of the compiler's field. The default value is 8. C ++ is a smaller method than its own size. For example, if you specify that the compiler is bounded by 2 pairs and the size of the int type is 4, the int pair is bounded by 2 and 2, which is smaller than 4. In the default method, because almost all data types are not greater than the default method 8 (except long double ), therefore, all inherent types of peer methods can be considered as the size of the type itself. ChangeProgram:

# Pragma pack (2)
Union U2
{
Char A [13];
Int B;
};

Union U3
{
Char A [13];
Char B;
};
# Pragma pack (8)

Cout <sizeof (U2) <Endl; // 14
Cout <sizeof (U3) <Endl; // 13

Because the method of Manually changing to 2 is also changed to 2 for int, and the biggest Member for U2 is 2, so now sizeof (U2) = 14.

Conclusion:C ++ is a smaller method than its own size.

9. sizeof problem of struct

The sizeof structure is complicated due to alignment. See the following example: (the default alignment Mode)

Struct S1
{
Char;
Double B;
Int C;
Char D;
};

Struct S2
{
Char;
Char B;
Int C;
Double D;
};

Cout <sizeof (S1) <Endl; // 24
Cout <sizeof (S2) <Endl; // 16

It is also two Char Types, one int type and one double type, but their sizes are different due to the bounded problem. The element pendulum method can be used to calculate the struct size. For example, the CPU determines the peer bounds of the struct. According to the conclusion in the previous section, both S1 and S2 have the largest element type, that is, double type's ter8. Then, each element is placed.

For S1, first place a to the peer interface of 8, assuming that it is 0. At this time, the next idle address is 1, but the next Element D is of the double type, the closest address to 1 is 8, so d is placed in 8. At this time, the next idle address becomes 16, and the peer interface of the next element C is 4 or 16, therefore, C is placed at 16. At this time, the next idle address is changed to 20. The next element D needs to be bound to 1, which also falls in the opposite world. Therefore, D is placed on 20, the struct ends at address 21. Because the size of S1 must be a multiple of 8, the space from 21-23 is retained, and the size of S1 is changed to 24.

For S2, first place a to the peer interface of 8, assuming that it is 0, then the next idle address is 1, and the peer interface of the next element is also 1, so B is placed in 1, the next idle address is changed to 2; the peer interface of the next element C is 4, so take the address closest to 2 4 and place it in C. The next idle address is changed to 8, the peer interface of the next Element D is 8, so d is placed in 8. After all the elements are placed, the struct structure ends at 15 points, occupying a total space of 16, which is exactly a multiple of 8.

There is a trap here. For struct members in the struct, do not consider its alignment as its size. See the following example:

Struct S1
{
Char A [8];
};

Struct S2
{
Double D;
};

Struct S3
{
S1 S;
Char;
};

Struct S4
{
S2 S;
Char;
};

Cout <sizeof (S1) <Endl; // 8
Cout <sizeof (S2) <Endl; // 8
Cout <sizeof (S3) <Endl; // 9
Cout <sizeof (S4) <Endl; // 16;

The size of S1 and S2 is 8, but the alignment of S1 is 1 and S2 is 8 (double). Therefore, this difference exists in S3 and S4.

Therefore, when you define a struct, if the space is insufficient, consider alignment to arrange the elements in the struct.

 

Conclusion:Elements in struct are stored sequentially. The number of bytes occupied by each element is determined by the number of aligned bytes (N) (the most occupied element in struct is smaller than the number of bytes aligned with the CPU) make adjustments. if M elements from left to right add up more than n Bytes, then remove k elements from right to left until M-K elements add up less than or equal to N, if n is equal to N, NO byte filling is required. If n is less than N, the elements of the M-K-1 are filled until = n.

Address: http://blog.csdn.net/zzffly9/article/details/1844421

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.