How the C, C ++ compiler processes the struct size

Source: Internet
Author: User

1. What is alignment and why:

1. 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.

2. Alignment function and cause: Each hardware platform has a big difference in processing the storage space. 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 suitable for their platforms will result in a loss of access efficiency. For example, some platforms start from the even address each time they read data. If an int type (assuming 32-bit) 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, it may take two read cycles and splice the high and low bytes of the two read results to obtain the int data. Obviously, reading efficiency is greatly reduced. This is also a game of space and time.

Alignment (offset of the starting address of the variable to the starting address of the structure)

Char
The offset must be a multiple of sizeof (char), that is, 1.

Int
The offset must be a multiple of sizeof (INT), that is, 4.

Float
The offset must be a multiple of sizeof (float), that is, 4.

Double
The offset must be a multiple of sizeof (double), that is, 8.

Short
The offset must be a multiple of sizeof (short), that is, 2.

 

When each member variable is stored, the space is requested in sequence based on the order in which the structure appears, and the positions are adjusted according to the alignment above. The vacant byte VC is automatically filled. At the same time, to ensure that the size of the structure is a multiple of the number of byte boundaries (that is, the number of bytes occupied by the Type occupying the maximum space in the structure, therefore, after applying for space for the last member variable, the vacant bytes will be automatically filled as needed.

Ii. Alignment implementation

Usually, we do not need to consider alignment when writing a program. The compiler selects an alignment policy suitable for 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.
Struct mystruct

{

Double dda1;

Char DDA;

Int type

};

When allocating space for the above structure, VC allocates space for the first member dda1 according to the sequence and alignment of the member variables, the starting address is the same as the starting address of the structure (the offset 0 is just a multiple of sizeof (double). The member variable occupies eight bytes; next, allocate space for the second member DDA. the offset of the next address that can be allocated to the starting address of the structure is 8, which is a multiple of sizeof (char, therefore, the DDA is stored in an alignment where the offset is 8. This member variable occupies sizeof (char) = 1 byte, and then allocates space for the third member type, in this case, the offset of the next allocable address to the starting address of the structure is 9, not a multiple of sizeof (INT) = 4. To meet the offset constraints of alignment, VC automatically fills in three bytes (these three bytes do not have anything). At this time, the next address that can be allocated has a 12 offset to the starting address of the structure, which is exactly sizeof (INT) = a multiple of 4, so the type The member variable occupies sizeof (INT) = 4 bytes, and the member variables in the entire structure are allocated space. The total occupied space is: 8 1 3 4 = 16, which is a multiple of the number of byte boundary values of the structure (that is, the number of bytes occupied by the Type occupying the maximum space in the structure) sizeof (double) = 8, therefore, no vacant bytes need to be filled. Therefore, the size of the entire structure is sizeof (mystruct) = 8 1 3 4 = 16, three of which are automatically filled by VC, without any meaningful content.

Let's take another example, for example:
For example, the following structure shows the allocation of member spaces.


Struct

Test {
Char x1;
Short X2;
Float X3;
Char X4;
};
The first member of the structure X1, whose offset address is 0, occupies 1st bytes. The second member X2 is of the short type, and its starting address must be two byte pairs. Therefore, the compiler fills a Null Byte between X2 and X1. The third member X3 and fourth member X4 of the structure exactly fall on their natural peer address, and no additional bytes are needed before them. In the test structure, the X3 member requires a 4-byte bounded boundary and is the maximum boundary unit required by all the members of the structure. Therefore, the natural boundary condition of the test structure is 4 bytes, the compiler fills in three NULL bytes after Member X4. The entire structure occupies 12 bytes of space.

The details of byte alignment are related to compiler implementation, but generally three criteria are met:


1) The first address of the struct variable can be divisible by the size of its widest basic type member;
2) The offset (offset) of each member of the struct to the first address of the struct is an integer multiple of the member size. If necessary, the compiler will add the internal adding between the members );
3) the total size of the struct is an integer multiple of the size of the widest basic type of the struct. If necessary, the compiler will add the trailing padding after the last member ).

Note:

The basic type refers to the built-in data types such as char, short, Int, float, and double. The "data width" here refers to the size of its sizeof. Because the struct member can be a composite type, for example, another struct, when looking for the widest basic type member, it should include the Child Member of the composite type member, instead of seeing a composite member as a whole. However, when determining the offset of a composite member, the composite type is regarded as a whole.

Change the default Allocation Policy of the C Compiler
Generally, you can use the following method to change the default peer condition:
Use pseudoinstructions # pragma pack ([N])
# Pragma pack ([N]) pseudoinstructions allow you to select the peer policy adopted by the compiler to allocate space for data.
For example, after the # pragma pack (1) Directive is used, the space allocation of members in the test structure is aligned by one byte. The format is as follows:
# Pragma pack (push) // save alignment status
# Pragma pack (1)
// Define your structure
//............
# Pragma pack (POP)

 

With the above explanation, I believe you should have a clear understanding of the concept of byte alignment in C language. In network programs, it is very important to master this concept. A binary stream (such as a struct) is transmitted between different platforms (such as between Windows and Linux ), therefore, the same alignment method must be defined between the two platforms. Otherwise, some errors may occur, but it is difficult to troubleshoot.

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.