Alignment of struct members

Source: Internet
Author: User

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 types of 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.

(1) symptom

For example, some platforms start from the even address each time they read data. If an int type (assuming a 32-bit System) is stored at the beginning of the even address, the 32bit can be read in a read cycle, if the data is stored at the beginning of the odd address, two read cycles are required, and the high and low bytes of the two read results are pieced together to obtain the 32bit data. Obviously, reading efficiency is greatly reduced. Let's look at the following example:

32-bit machine, 4-byte alignment by default

Struct

{

Int;

Char B;

Short c;

};

Struct B

{

Char B;

Int;

Short c;

};

Main ()

{

Struct A x;

Struct B y;

Printf ("% d", sizeof (x), sizeof (y ));

}

The result is 8, 12. It is reasonable to say that the two should be the same. Is it 7? The above result is displayed because the compiler needs to align data members in space. For A, a occupies 4 bytes, B occupies 1 byte, c occupies 2 bytes, B and c can be placed in A 4 byte, and the minimum allocation is 4, so 8 is occupied; for B, B occupies one byte, but it cannot accommodate a, so a occupies the other 4 bytes. B can only occupy 4 bytes, and c can only occupy 4 bytes, that is, there is no sharing, so it accounts for 12. The visible order has an impact.

The above is the result of alignment according to the default settings of the compiler. Can we change the default alignment settings of the compiler? Of course.

# Pragma pack (2)
Struct B
{
Char B;
Int;
Short c;
};

Int _ tmain ()
{
B y;
Printf ("% d", sizeof (y ));
}

The output value is 8, because it is a 2-byte alignment, which can be allocated at least 2 bytes. B occupies 2 bytes, a occupies 4 bytes, and c occupies 2 bytes. Similar to the following:

# Pragma pack (1)
Struct B
{
Char B;
Int;
Short c;
};

Int _ tmain ()
{
B y;
Printf ("% d", sizeof (y ));
}

The output value is 7. Because 1 byte is aligned, the minimum value can be 1. B occupies one byte, a occupies 4 bytes, and c occupies 2 bytes.

(2) Induction of principles

The principle of alignment is as follows:
1. Alignment value of the Data Type itself: For char data, its alignment value is 1, for short data is 2, for int, float, double type, its own alignment value is 4 in bytes.
2. The alignment value of a struct or class: The value with the largest alignment value among its members.
3. Specify the alignment value: # The alignment value specified when pragma pack (value) is used.

(3) how to use
How do I modify the default alignment of the compiler?

1. In vc ide, you can modify it as follows: [Project] | [Settings], Struct Member Alignment of the Code Generation option of the c/c ++ tab Category, the default value is 8 bytes.

2. You can modify the code dynamically as follows: # pragma pack.

3. If we want to save space during programming, we only need to assume that the first address of the structure is 0, and then sort the variables according to the above principles. The basic principle is to declare the variables in the structure according to the type size from small to large, and minimize the space to fill. Another method is to exchange space for time efficiency. For example, a space for time is used to explicitly insert reserved members:
Struct {
Char;
Char reserved [3]; // use space for time
Int B;
}
The reserved member has no significance for our program. It just fills the space to achieve byte alignment. Of course, even if this member is not added, the compiler will automatically fill the alignment for us, we add it as an explicit reminder.

From http://blog.csdn.net/sky0829/article/details/6010482

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.