C/C ++ structure alignment Summary

Source: Internet
Author: User

When I looked at the C ++ object model, I encountered several memory la s that all had some details about alignment. Therefore, I made a summary of the structure alignment. Someone said: the structure alignment is dependent on the compiler, so you don't need to study it. Is it true?

 

Maybe, maybe not. It depends on the industry you are working in. If you are working in system formation, network communication, embedded systems, and one byte saving, maybe you have a lot of expectations. Although the specific Alignment Method varies with the compiler, the basic principle of alignment remains unchanged. That principle may guide us to follow a certain principle when writing a program.

 

However, since you use C or C ++, most of them are at the bottom of the system. What do you mean? Haha.

 

Now let's talk about the alignment rules of the MS vs2005 Compiler (cl.exe for x86) under Windows x86 32-bit Host:

 

For example, the following struct definition:

 

Struct

{

Int I;

Double D;

Char C;

};

 

Q: What is the size of sizeof (struct a) in vs2005?

 

Now let's talk about how cl.exe (the Microsoft vs2005 compiler process) is implemented by default:

 

1. Determine the alignment quantity: Find the maximum size of the basic type member in A, which is 8 (double size) in this example ).

 

2. When a struct variable struct a AA; is defined, the starting address of AA is divisible by alignment determined by 1. In this example, the starting address of AA must be divisible by 8)

 

2. Then, allocate int I, 4 bytes of space, and double D. Note that double is 8 bytes, so it must be allocated where it is divisible by 8, therefore, 4-byte filling is left behind int I;

 

3. Then, assign char C; 1 byte. The size of struct a is 4 (this is int I;) + 4 (this is filled) + 8 (this is double D;) + 1 (this is Char C;) = 17;

 

4. Finally, it is required that the total size of the struct be divisible by the alignment determined by 1. Here, the size of struct a should be divisible by 8, therefore, we need to add a 7-byte character, which is a total of 24 bytes.

 

It can be seen from this that the structure of vs2005 compiler is filled with such a rule (by default, this rule can be changed by the option above the project property ):

 

1. Determine alignment: the maximum number of bytes of data members in the struct.

 

2. When defining a struct variable, the starting address must be divisible by a fixed alignment;

 

2. When each member is allocated, the offset of the member relative to the starting address must be divisible by the size of the member;

 

3. The total size of the struct can be determined by the alignment;

 

 

 

The extended byte is pad (padding byte)

 

 

Next, let's take a look at the default rules of the Linux g ++ 3.4.3 compiler under the x86 32-bit host, which is relatively simple: (equivalent to changing the alignment to 4-byte alignment in vs2005)

 

The default alignment mode is 4 bytes, so each member can be aligned in 4 bytes. The size of the above struct is:

 

4 (int I; size) + 8 (double size) + 1 (char size) + 3 (Supplement byte for 4 bytes) = 16 bytes;

 

The features assigned to the first address of the GCC struct variable cannot be found at the moment. Thank you.

 

Finally, if you define a struct without any Members, what is the size of struct? It is 1 byte. If two struct variables struct a A1, A2, and 1 are defined, the A1 and A2 addresses can be distinguished!

 

There are other compilers and operating systems that I don't know.

 

These rules may not be the same, but they give us a point of attention during programming:

 

When defining a struct, it is best for Members to define it from large to small, which can save space relatively. (At least it will not be worse than other orders. Generally, ha .)

 

For example, if the above struct can be defined as follows:

 

Struct

{

Double D;

Int I;

Char C;

};

 

Therefore, both vs2005 in windows and GCC in Linux are 16 bytes.

 

Alignment can also be changed in the Code through the features provided by various compilers. However, if I have never used alignment, I have no right to speak. You need to refer to other articles.

 

The above process and inference are the results of other articles and machine tests. If there is anything wrong, please kindly advise. Thank you.

 

 

I am not sure about the alignment under vs2005 in case of nesting, but my analysis is as follows:

 

Struct

{

Char C;

Double D;

Int I;

};

 

Struct B

{

Struct a;

Char C;

};

 

In this case, analyze the size of B;

 

1. Determine the alignment quantity: including the comparison of members in a and recursion to compare the size of members of each basic type. The maximum value is 8 (the size of double );

 

2. The starting address is the same as the preceding one. The actual address of the variable B must be divisible by 8;

 

3. The offset of struct a A; in B is the same as that when a is used as the struct variable separately (this facilitates the assignment operation)

 

The remaining rules are the same as those above;

 

Therefore, the size of B is 24 (struct a) + 1 (char) + 7 (PAD) = 32 bytes;

 

Are they correct? Please also 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.