pragma pack (very useful byte alignment usage instructions)

Source: Internet
Author: User

Http://hi.baidu.com/zhanghuikl/blog/item/124ea7998108a9006e068c19.html

Emphasize one point:

#pragma packs (4)

typedef struct

{

Char buf[3];

Word A;

}KK;

#pragma pack ()

The principle of alignment is min (sizeof (word), 4) = 2, so it's 2-byte alignment, not the 4-byte alignment we think.

Here are three important points:
1. Each member is aligned in its own way and can minimize the length of
2. The default alignment of a complex type, such as a struct, is the alignment of its longest member, so that when the member is a complex type, the length can be minimized
3. The length after alignment must be an integer multiple of the largest alignment parameter in the member, so that each item is guaranteed to be aligned when the array is processed

To add, for arrays, for example:
Char a[3]; this way, its alignment is the same as writing 3 chars, respectively. That is, it is still aligned in 1 bytes.
If write: typedef char ARRAY3[3];
Array3 This type of alignment is still aligned to 1 bytes instead of its length.
Regardless of the type, the aligned boundary must be 1,2,4,8,16,32,64 ...

Statement:
Organize your posts from the Internet to people, and part of the reference to MSDN.

Role:
Packing alignment of the specified structure, union, and class members;

Grammar:
#pragma pack ([show] | [Push | pop] [, identifier], N)

Description
1,pack provides data declaration level control and does not work on definitions;
2, the call pack does not specify the parameters, N will be set to the default value;
3, once changes the data type alignment, the direct effect is occupies the memory reduction, but performance will descend;

Syntax Specific analysis:
1,show: Optional parameter showing the number of bytes in the current packing aligment, displayed as warning message;
2,push: Optional parameter, the current specified packing alignment value of the stack operation, where the stack is the internal compiler stack, while setting the current packing alignment to n; if n is not specified, Then the current packing alignment the numerical pressure stack;
3,pop: optional parameter; Deletes the topmost record from the internal compiler stack; If n is not specified, the current stack top record is the new packing alignment value, and if n is specified, then n becomes the new packing aligment numeric value; If identifier is specified, the record in internal compiler stack will be pop until identifier is found, then pop out identitier and set packing The alignment value is the record at the top of the current stack; If the specified identifier does not exist in the internal compiler stack, the pop operation is ignored;
4,identifier: optional parameter; When used with push, give the record a name that is currently pressed into the stack, and when used with the pop, from internal compiler Pop out all the record in stack until identifier is pop out, if identifier is not found, then ignore the pop operation;
5,n: optional parameter; Specifies the number of packing, in bytes, the default value is 8, and the legal values are 1, 2, 4, 8, 16.

Important rules:
1, the members of a complex type are stored sequentially in memory in the order in which they are declared, with the address of the first member being the same as the address of the entire type;
2, each member is aligned, that is, each member is aligned in its own way, and the length is minimized; The rule is that each member is aligned to its type (usually the size of this type) and the smaller of the specified alignment parameter;
3, the structure, union or class of data members, the first place in the offset of 0; the alignment of each data member, followed by the number specified in the #pragma pack and the smaller of the data member's own length two; that is, when #pragma The size of the specified value will not produce any effect when the value specified by the pack equals or exceeds the length of all data members;
4, the alignment of complex types (such as structs) as a whole is performed according to the smaller value between the largest data member in the structure and the value of the #pragma pack, so that the length can be minimized when the member is a complex type;
5, the calculation of the overall length of the structure must take the integer times of all the alignment parameters used, not enough to fill the empty byte; that is, to take the integer multiple of the largest value in all the alignment parameters used, because the alignment parameter is 2 n-th, so that when the array is processed, each item is bound to be aligned;


To change the default byte alignment for the C compiler:
By default, the C compiler allocates space for each variable or data unit according to its natural boundary condition; Generally, you can change the default boundary condition by using the following two methods:
Method One:
Using the #pragma pack (n), specifies that the C compiler is aligned according to n bytes;
Use the #pragma pack () to cancel the custom byte alignment.

Method Two:
__attribute (aligned (n)) to align the data members that are acting on the natural boundary of n bytes, and if the length of a member in the structure is greater than N, it is aligned according to the length of the maximum member;
__attribute ((packed)), cancels the optimized alignment of the structure during compilation, and aligns to the actual number of bytes occupied.

To sum up, the following examples and detailed analysis:

Example one:
#pragma packs (4)
Class Testb
{
Public
int AA; The first member, placed in the position of [0,3] offset,
Char A; The second member, which has a length of 1, #pragma pack (4), takes a small value, which is 1, so this member is aligned in one byte and placed in the position of offset [4].
Short B; The third member, itself 2, #pragma pack (4), 2, and 2 bytes aligned, so put in the position of offset [6,7].
char c; Fourth, its own length is 1, placed in [8] position.
};
Visible, this class actually occupies 9 bytes of memory space. According to Rule 5, the overall alignment of the structure is min (sizeof (int), pack_value) = 4, so sizeof (TESTB) = 12;


Example two:
#pragma packs (2)
Class Testb
{
Public
int AA; The first member, placed in the position of [0,3] offset,
Char A; The second member, which has a length of 1, #pragma pack (4), takes a small value, which is 1, so this member is aligned in one byte and placed in the position of offset [4].
Short B; The third member, itself 2, #pragma pack (4), 2, and 2 bytes aligned, so put in the position of offset [6,7].
char c; Fourth, its own length is 1, placed in [8] position.
};
The results are the same as the example, the position of each member has not changed, but at this time the overall alignment of the structure is min (sizeof (int), pack_value) = 2, so sizeof (TESTB) = 10;


Example three:
#pragma packs (4)
Class TESTC
{
Public
Char A; The first member, placed in the [0] offset position,
Short B; The second member, itself 2, #pragma pack (4), 2, and 2 bytes aligned, so put in the position of offset [2,3].
char c; The third one, the length of itself is 1, placed in [4] position.
};
The actual memory consumption of the entire class is 5 bytes, the whole is aligned according to Min (sizeof (short), 4) = 2, so the result is sizeof (TESTC) = 6;


Example four:
struct Test
{
Char X1; The first member, placed in the [0] position,
Short X2; The second member, which has a length of 2, is aligned by 2 bytes, so it is placed in the position of the offset [2,3].
Float X3; The third member, which has a length of 4, is aligned by 4 bytes, so it is placed in the position of the offset [4,7].
Char x4; The fourth Chen Kuan, which has a length of 1, is aligned by 1 bytes, so it is placed in the position of offset [8],
};
So the actual memory consumption of the entire structure is 9 bytes, but considering that the overall alignment of the structure is 4 bytes, the space occupied by the entire structure is 12 bytes.


Example five:
#pragma packs (8)

struct S1
{
Short A; The first one, placed in [0,1] position,
Long B; The second, the length is 4, by min (4, 8) = 4 aligned, so put in [4,7] Position
};
So the actual memory consumption of the structure is 8 bytes, the alignment of the structure is min (sizeof (long), pack_value) = 4 bytes, so the space occupied by the whole structure is 8 bytes.

struct S2
{
char c; The first one, placed in [0] position,
S1 D; Second, according to rule four, alignment is min (4, pack_value) = 4 bytes, so put in [4,11] position,
Long long E; The third, with its own length of 8 bytes, is aligned by 8 bytes, so put it in the [16,23] position,
};
So the actual memory consumption is 24 of its own, the overall alignment is 8 bytes, so the entire structure occupies 24 bytes of space.

#pragma pack ()
So:
sizeof (s2) =, s2 after c is empty 3 bytes followed by D.

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.