Note: Member alignment has an important condition, that is, each member is aligned in its own way. the alignment rule is that each member has a smaller alignment according to its type alignment parameter (usually the size of this type) and the specified alignment parameter (8 bytes by default. in addition, the length of the structure must be an integer multiple of all alignment parameters used.
The sizeof value of the struct (struct) does not simply add the bytes occupied by each element, but takes into account the byte alignment of the storage space. Let's take a look at the two struct types defined below.
Struct
{
Char;
Short B;
Char c;
} S1; struct
{
Char;
Char B;
Short c;
} S2; the sizeof (S1) = 6 and sizeof (S2) = 4 are visible by program testing respectively. Although the elements of the two struct are the same, however, because the type order of the elements in the table is different, the occupied bytes are also different. This is the reason for byte alignment. Byte alignment helps to speed up the computer's data acquisition speed. Otherwise, the instruction cycle will be much higher. Byte alignment principle the default byte alignment of a struct generally meets three criteria: 1) the first address of the struct variable can be divisible by the size of its widest basic type member;
2) The offset between each member of the struct and the first address of the struct, that is, the starting address of each member) is an integer multiple of the member's own size, if necessary, the compiler will add the padding byte 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 padding byte trailing padding after the last member ).
Note: When there are several group members in the struct, such as int a [10], 10 integer variables must be considered for calculation.
Through these three principles, it is not difficult to understand the difference between the above two struct. for struct S1, in order to make the short variable conform to the byte rules (2), that is, the offset of its storage location relative to the first address of the struct is its own size (short occupies 2 bytes) must be filled with one byte after byte a to align; then by the criterion (3), in order to meet the total size of the struct to be an integer multiple of the short size, A byte must be added after c.
For struct S2, you do not need to fill the bytes as described above, because the direct sequential storage already meets the alignment criterion. If we change the short values of the above two struct to int (4 bytes), what will happen? The program obtains sizeof (S1) = 12, sizeof (S2) = 8
Using the above principles, it is not difficult to calculate such a result. In S1, 3 bytes are entered after a and 3 bytes are entered after c, so that a total of 12 bytes are entered. in S2, after a and B are stored in sequence, 2 bytes are filled for it, in this way, there are 8 bytes in total. Of course, you can also set the byte alignment mode in some cases. This requires the # pragma pack.
# Pragma pack (push) // Save the pressure Stack
# Pragma pack (1) // set 1-byte alignment
Struct
{
Char;
Short B;
Char c;
} S1;
# Pragma pack (pop) // restore the previous settings as shown above. If the method is set to 1-byte alignment, S1 is not filled with bytes, and sizeof is the sum of the bytes occupied by each element, that is, 4. This is useful when reading struct-sized data from an external binary file to struct. In addition, there is also a method as follows:
· _ Attribute (aligned (n) to align the structure members to the natural boundary of n Bytes. If the length of a member in the structure is greater than n, the maximum member length is used for alignment.
· _ Attribute _ (packed): cancels the optimization alignment of the structure during compilation and alignment according to the actual number of bytes occupied.
This article is from the "energy of Combustion Technology" blog, please be sure to keep this source http://boyishachang.blog.51cto.com/3485129/1273323