Byte alignment problems caused by struct members in the C Language

Source: Internet
Author: User

What is the byte alignment of the struct, I will not repeat it, here is an article about byte alignment: http://www.linuxsong.org/2010/09/c-byte-alignment/.

Here, all the data types of the structure byte alignment are basic data types. What if the structure definition contains the structure member?

Many people write blogs on the Internet to discuss this issue. They all think that the structure members should be regarded as a whole, and the bytes should be aligned according to the total number of bytes and the first address should be selected. However, after testing, this statement is incorrect.

                               };

For the above Code, apparently sizeof (struct s1) = 4. If you think of struct s1 as a whole for byte alignment, the sizeof (struct s2) should be 8. However, the test results of this Code in gcc and VS2010 are both 5.

Therefore, the memory layout of s2 is as follows:

S2: char |

The first address of s is not 4, but an integer multiple of the char length.

Similarly, for struct:

                           };

In the above Code, sizeof (struct S1) = 4, while sizeof (struct S2) = 6.

The memory layout of S2 is:

S2: char ***** | char ***** short

The first address of s is not 4, but an integer multiple of the short Type length. The S2 length of the struct is not an integer multiple of the S1 length, but an integer multiple of the short Type length.

Relatively, we can look at the struct:

 

Sizeof (S) = 4.

Its memory layout is:

S: char short

Comparing S2 and S, we can see that the s struct member in S2 is indeed still laid out according to its overall position as the S1 struct (occupying 4 bytes.

 

Then we can draw the conclusion of the byte alignment problem in this case:

1) struct members are arranged in the new struct according to the memory layout of the original struct;

2) The first memory address of a struct member is an integer multiple of the longest Data Type of the member;

3) the overall length of the struct has nothing to do with the struct members, but an integer multiple of the longest data type in the struct (including the struct members.

 

After the conclusion is drawn, we will go back to the processor itself to discuss the reason why the compiler is doing this.

From the perspective of CPU structure, there is an easy-to-understand article on memory alignment:

Http://hi.baidu.com/maikeai/item/4976f24cc0f905d3c1a592a0

From this article, we can know that the reason for memory alignment is that the CPU needs to reduce the number of times the memory is read when reading data and improve efficiency.

A 32-bit CPU can read up to 32-bit dual-word data at a time. For 32-bit data, the memory must be read multiple times on a 32-bit computer, this is irrelevant to whether bytes are aligned.

We can see that byte alignment is actually taking the storage space and changing the CPU time.

Therefore, the built-in data types in the C language can be well aligned with the memory to save time, And the wasted memory space is also controllable.

However, for a struct Member, because its length is uncontrollable, if it is forcibly aligned according to the overall memory, the first reason may be that the length is too long, even if the memory alignment requires a large number of memory reads, the optimization time is not obvious, and the memory space may be too large. Therefore, the simplest way to balance space and time is to align bytes according to the longest Data Type of the struct, which is most likely to reduce the number of memory reads, it also makes the wasted storage space controllable.

 

End

 

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.