Struct Word Alignment:-_-It is thought of by an answer to longxun!

Source: Internet
Author: User

In the past few days, I have prepared a reading question, thought of the struct alignment problem, and shared it with you.
1. Interview Questions:
Typedef union student
{
Char name [10];
Long SnO;
Char sex;
Float score [4];
} Stu;
Main ()
{
Stu a [5];
Printf ("% d/N", sizeof ());
}
The answer is 80, because union is variable and takes the largest member of the Union as the size of the Union!
But what is the change to struct?
Typedef struct student
{
Char name [10];
Long SnO;
Char sex;
Float score [4];
} Stu;
Main ()
{
Stu a [5];
Printf ("% d/N", sizeof ());
}
The answer is 180 ???? Why not (10 + 4 + 1 + 16) x 5 = 155? Because struct has an issue called alignment:
Non-Alignment Data Access affects the speed on x86, because in case of non-alignment, small data may affect the efficiency. Alignment means allocating more bytes and filling useless data, change the consumption rate with space loss.
Struct is a composite data type. Its components can be both basic data types (such as int, long, float, and so on) variables, it can also be a data unit of some composite data types (such as array, struct, Union, etc. For struct, the compiler automatically alignment member variables to improve the computing efficiency. By default, the compiler allocates space for each member of the struct according to its natural alignment condition. Each member is stored in the memory in the declared order. The address of the first member is the same as that of the entire structure.
Natural alignment (default alignment) refers to the basic allocation unit based on the members with the largest (type) size in the struct, which is closely related to the order.
For example:
Struct naturalalign
{
Char;
Short B;
Char C;
};
In the above struct, short is the largest in size and the length is 2 bytes. Therefore, char members A and C in the struct are aligned in units of 2. sizeof (naturalalign) the result is 6;
If changed:
Struct naturalalign
{
Char;
Int B;
Char C;
};
The result is obviously 12.
Then return to the original question: In the struct, the maximum size is long and the size is 4. Therefore, in order, char name [10]; 12 bytes; long SnO; 4 bytes; char sex; 4 bytes (aligned here); float score [4]; 16 bytes. So (12 + 4 + 4 + 16) × 5 = 180, that's it!
As I said earlier, it depends on the sequence. Let's change it:
Typedef struct student
{
Char name [10];
Char sex;
Long SnO;
Float score [4];
} Stu;
Main ()
{
Stu a [5];
Printf ("% d/N", sizeof ());
}
The answer is: 160. Why is the order changed? The key lies in order.
In the struct, the maximum size is long and the size is 4. Therefore, in order, char name [10]; 12 bytes; however, the two allocated bytes in the 12 can contain the subsequent char sex; (the problem lies here); float score [4]; 16 bytes. So (12 + 4 + 16) × 5 = 160, that's it! So be careful!
 
2. Specify alignment
Generally, you can change the default alignment conditions using the following method:
· Using Pseudo commands # pragma pack (n), the compiler will be aligned according to n Bytes;
· Use the pseudo command # pragma pack () to cancel the custom byte alignment.
Note: If the value of N specified in # pragma pack (n) is greater than the size of the maximum member (type) in the structure, the structure still performs Alignment Based on the members with the largest size.
For example:
# Pragma pack (N)
Struct naturalalign
{
Char A; // 2B
Int B; // 4B
Char C; // 2B
};
# Pragma pack ()
When N is 4, 8, or 16, the alignment is the same, and the sizeof (naturalign) result is 12. When N is 2, it plays a role, making sizeof (naturalalign) Result 8.
In the VC ++ 6.0 compiler, we can specify the alignment mode. The operation mode is to select the projetct> setting> C/C ++ menu in sequence, specify the peer mode in struct member alignment.
In addition, through _ attribute (aligned (N), the struct member can be aligned on the N-byte boundary, but it is rarely used, therefore, we will not explain it in detail.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/cxyol/archive/2005/09/25/489104.aspx

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.