Boundary alignment of struct

Source: Internet
Author: User

Boundary alignment of struct
Companies such as Intel and Microsoft once had a similar interview question:

1. # include <iostream. h>

2. # pragma pack (8)
3. struct example1
4 .{
5. Short;
6. Long B;
7 .};

8. struct example2
9 .{
10. Char C;
11. example1 struct1;
12. Short E;
13 .};
14. # pragma pack ()

15. Int main (INT argc, char * argv [])
16 .{
17. example2 struct2;

18. cout <sizeof (example1) <Endl;
19. cout <sizeof (example2) <Endl;
20. cout <(unsigned INT) (& struct2.struct1)-(unsigned INT) (& struct2) <Endl;

21. Return 0;
22 .}

What is the input result of the program?

The answer is:

8
16
4

Do not understand? Still do not understand? Here is one by one:

2.1 Nature

Struct is a composite data type, which can be a variable of the basic data type (such as int, long, float, and so on) or a composite data type (such
Array, struct, Union, etc. For struct, the compiler automatically alignment member variables to improve the computing efficiency. By default, the compiler is
Natural
Alignment) condition to allocate space. Each member is stored in the memory in the declared order. The address of the first member is the same as that of the entire structure.

Natural alignment (natural alignment) is the default alignment, which refers to Alignment Based on the largest member of the struct.

For example:

Struct naturalalign
{
Char;
Short B;
Char C;
};
In the above struct, short is the largest in size and the length is 2 bytes. Therefore, char members A and C in the struct are aligned in units of 2. sizeof (naturalalign) the result is 6;

If changed:

Struct naturalalign
{
Char;
Int B;
Char C;
};
The result is obviously 12.

2.2 specify the peer

Generally, you can use the following method to change the default peer condition:

· Using Pseudo commands # pragma pack (n), the compiler will be aligned according to n Bytes;
· Use the pseudo command # pragma pack () to cancel the custom byte alignment.

Note: If the value of N specified in # pragma pack (n) is greater than the size of the largest member in the structure, the structure does not take effect, and the structure is still bounded by the member with the largest size.

For example:

# Pragma pack (N)
Struct naturalalign
{
Char;
Int B;
Char C;
};
# Pragma pack ()
When N is 4, 8, or 16, the alignment is the same, and the sizeof (naturalign) result is 12. When N is 2, it plays a role, making sizeof (naturalalign) Result 8.

In the VC ++ 6.0 compiler, we can specify its peer mode, and choose projetct> setting> C/C ++ menu in sequence, specify the peer mode in struct member alignment.

In addition, through _ attribute (aligned (N), the struct member can be aligned on the N-byte boundary, but it is rarely used, therefore, we will not explain it in detail.

2.3 answers to interview questions

So far, we can answer intel and Microsoft interview questions comprehensively.

Line 1 in the Program # pragma pack (8), although the interface is specified as 8
The maximum member size in example1 is 4 (long variable size is 4), so struct example1 is still bounded by 4 bytes, struct
The size of example1 is 8, that is, the output result of 18th rows;

Struct example2 contains struct example1. the maximum size of simple data members contained in struct example1 is 2 (short variable E), but it contains struct example1, in struct example1, the maximum member size is 4, and struct example2 should also be 4 pairs. Because 8 is greater than 4, # pragma pack (8) the peer field specified in does not work for struct example2, so the output result of 19 rows is 16;

Because the members in struct example2 are 4-aligned, the char variable C should be followed by three null values, and the latter is the memory space of the member struct1, the output result of 20 rows is 4.

 

Note: The above is not very clear. To sum up, by default, VC specifies that the offset between the starting address and the starting address of each member variable to the starting address of the structure must be the number of bytes occupied by this type
. In addition, to ensure that the size of the structure is the number of boundary bytes (the maximum space occupied by the structure)
The number of bytes occupied by the type), so after applying for space for the last member variable, the vacant bytes will be automatically filled as needed. (Supermonkey note: in fact, this is equivalent to expanding example1 members to example2 to judge !)

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.