C ++ byte alignment

Source: Internet
Author: User

1. Origin

What is the size of struct {int a; char B;} for an interview question from BBS? The answer is 8. After reading it online, it is byte alignment.

2. Basic rules for byte alignment

First, the default alignment length of each type of variable is its own variable length. For example, if char occupies one byte, the alignment length is one byte, and int occupies four bytes, the alignment length is four bytes, the double occupies eight bytes, And the alignment length is 8. The actual meaning of an int alignment length of 4 is that the int variable must be stored in an address multiple of four.
The length of struct {char B; int a} is 8, because although B only occupies 1 byte, a must be stored in multiples of 4, therefore, all three bytes after B are discarded. Therefore, it takes 8 bytes to store B and.
The length of struct {int a; char B} is 8! Dizzy! The reason is as follows:
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 ).
Rule 1 controls the first address of the struct variable. It has nothing to do with the length of the struct variable.
Rule 2 controls the relative address of each variable in the structure, which is related to the length of the variable in the structure.
Rule 3 controls the overall length of the struct, which is related to the length of the struct variable.
It is precisely because of the third rule that the length of a struct must be an integer multiple of its longest variable length. Therefore, in the preceding example, it must be an integer multiple of 4, so it is 8.
If a struct contains a nested struct, you should note that the starting address of the struct variable is only an integer multiple of the widest basic internal type, rather than an integer multiple of the struct itself, and the length of the external struct, it is only the length multiple of the widest basic type.
For example:

Struct S1 {
Char c; // 1 byte
Int I; // The first three bytes are empty, occupying 4 bytes.
}; // Exactly 8 bytes, a multiple of 4
Struct S2 {
Char c1; // 1 byte
S1 s; // The first three bytes are empty, rather than the first seven bytes, occupying 8 bytes.
Char c2; // occupies 1 byte
}; // A total of 13 bytes. It must be a multiple of 4, followed by three more bytes, and then become 16 bytes.

3. Reasons for byte alignment

· Efficiency: Some platforms read data from even addresses each time. If an int type (assuming a 32-bit System) is stored at the beginning of an even address, A read cycle can read the 32bit data. If the data is stored at the beginning of the odd address, two read cycles are required, the 32-bit data can be obtained only when the high and low bytes of the two read results are pieced together. Obviously, reading efficiency is greatly reduced.

4. Hidden Dangers of byte alignment

· Unreasonable setting of variable definitions may result in a waste of memory

Struct S1 {// The length is 16
Char;
Int B;
Char c;
Int d;
}
Struct S2 {// The length is 12
Char;
Char c;
Int B;
Int d;
}

· The Byte alignment issue is not considered during address operations.

Char;
Int B;
Int * pb = & a + 1; // the address of a plus 1 is not the address of B. In fact, an ERROR occurs during compilation of this line of code, because the char * address cannot be assigned to the int * variable.

5. References

Structure byte alignment problem http://blogold.chinaunix.net/u1/49467/showart_424793.html
Byte alignment principle http://zhengyueyatou.blog.sohu.com/64814717.html

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.