#pragma pack (very useful byte alignment usage notes)

Source: Internet
Author: User

#pragma pack (4)//4-byte aligned, but in fact the maximum number of bytes consumed by a single member in a struct is 2 bytes, so it is actually aligned by 2 bytes

typedef struct

{

Char buf[3];

Word A;

}KK;

#pragma pack ()//de-Custom byte alignment

The principle of alignment is min (sizeof (word), 4) = 2, so it is 2-byte aligned instead of 4-byte alignment we think.

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

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

Statement:
Organize your posts from the Web, in part by referring to MSDN.

Role:
Specifies the structure, union, and packing alignment of the class members;

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

Description
1,pack provides control of the data declaration level, which does not work for the definition;
2, do not specify parameters when calling Pack, N will be set to default value;
3, once changing the data type of alignment, the direct effect is to occupy memory loss, but performance will fall;

Syntax specific analysis:
1,show: Optional parameters, displays the number of bytes currently packing aligment, is displayed as a warning message,
2,push: optional argument, and the currently specified packing Alignment values for the stack operation, where the stack is the internal compiler stack, while setting the current packing alignment is n; if n is not specified, the current packing alignment value is stacked;
3,pop: optional parameter; Removes the topmost record from the internal compiler stack, or if n is not specified, the current stack top record is the new packing alignment value, and if n is specified, n becomes the new packing aligment value, and if identifier is specified, the record in the internal compiler stack will be popped until identifier is found, and then the pop is identitier. Set the record of the packing alignment value to the top of the current stack, and if the specified identifier does not exist in the internal compiler stack, the pop operation is ignored;
4,identifier: Optional Parameters When used with push, gives a name of the record that is currently pressed into the stack, and when used with a pop, pops all the record from the internal compiler stack until the identifier is popped out. If identifier is not found, the pop operation is ignored;
5,n: optional parameter; Specifies the value of packing, in bytes,

Important rules:
1, in which each member of a complex type is stored sequentially in memory in the order in which they are declared, ;
2, each member is aligned separately, that is, each member aligns in its own way, and minimizes the length;
3, the data member of a struct, union, or class, first placed at an offset of 0; after each data member's alignment, follow #pragma The number specified by the pack is the size of the data member itself, which is the smaller of the two, and the means when the value specified by the #pragma pack equals or exceeds the length of all data members, The size of this specified value will have no effect;
4,
5, The overall length of the structure must take an integer multiple of all the alignment parameters used. Not enough to fill the empty byte, that is, to take an integer multiple of the largest value of all the alignment parameters used, because the alignment parameters are 2 of the n-th square; This ensures that each item is aligned at the same time as the array is processed;


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, and the default boundary conditions can generally be changed by the following two methods:
Method One:
Using #pragma pack (n), specify that the C compiler is aligned by n bytes;
Use #pragma pack () to cancel the custom byte alignment.

Method Two:
__attribute (aligned (n)), which aligns the data members to the natural boundary of N bytes, and aligns with the length of the largest member if the length of the member in the structure is greater than n;
__attribute ((packed)), cancels the optimization alignment of the structure during compilation, aligned according to the actual number of bytes consumed.

In summary, the following examples are given and analyzed in detail:

Example one:
#pragma pack (4)
class Testb
{
   public:
      int aa;    // The first member, placed at the [0,3] offset,
char A;  //second member, itself 1, #pragma pack (4), take a small value, that is, 1, so this member is aligned in one byte, placed in the position of the offset [4]. The next member is aligned 2 bytes, followed by an empty byte
short B;//third Member, self length 2, #pragma pack (4), take 2, 2 byte aligned, so placed in the offset [6,7] position.
Char C;  //fourth, self-length 1, placed at [8] position.
};
Visible, this class actually occupies 9 bytes of memory space.


Example two:
#pragma pack (2)
Class Testb
{
Public
int AA; The first member, placed in the [0,3] offset position,
Char A; The second member, which has a length of 1, #pragma pack (4), takes a small value, which is 1, so the member is aligned in one byte and placed in the offset [4] position. The next member is aligned in 2 byte, followed by an empty byte
Short B; The third member, which has a length of 2, #pragma pack (4), takes 2 and is aligned by 2 bytes, so it is placed in the offset [6,7] position.
char c; The fourth one, the self-length is 1, placed in [8] position.
};
The visible result is the same as the example, where the position of each member has not changed, but at this point the overall alignment of the structure is min (sizeof (int), pack_value) = 2, so sizeof (TESTB) = 10;char A is followed by an empty byte; char c Fill in an empty byte


Example three:
#pragma pack (4)
Class TESTC
{
Public
Char A; The first member, placed at the [0] offset, the next member is aligned by 2 bytes, followed by an empty byte
Short B; The second member, which has a length of 2, #pragma pack (4), takes 2 and is aligned by 2 bytes, so it is placed in the offset [2,3] position.
char c; The third one, its own length is 1, placed in [4] position.
};
The actual memory consumption of the whole 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. The next member is aligned 2 bytes, followed by an empty byte,
Short X2; The second member, with its own length of 2, is aligned by 2 bytes, so it is placed in the offset [2,3] position,
Float X3; The third member, with its own length of 4, is aligned by 4 bytes, so placed in the offset [4,7] position,
Char x4; The fourth member, with its own length of 1, is aligned by 1 bytes, so it is placed at offset [8],
};
So the actual memory consumption of the entire struct is 9 bytes, but considering the alignment of the structure as a whole is 4 bytes, the entire structure takes up 12 bytes of space. Char x1 and Short X2 share a 4-byte space, followed by an empty byte, and a char x4 after 3 empty bytes


Example five:
#pragma pack (8)

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

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 (since S1 occupies 8 bytes, why is this not 8 bytes?? ), so put it in [4,11] position,
Long long E; The third one, with a length of 8 bytes, is aligned by 8 bytes, so it is placed in the [16,23] position,
};
So the actual memory consumption is 24 bytes. The overall alignment is 8 bytes, so the entire structure takes up 24 bytes of space.

#pragma pack ()
So: sizeof (s2) =, char c is empty after 3 bytes followed by S1 D, S1 D followed by 4 empty bytes (here's a question??). As I understand it, char C accounts for one byte, followed by 7 bytes; S1 d accounts for 8 bytes; a long long e is 8 bytes)

#pragma pack (very useful byte alignment usage notes)

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.