#pragma pack (n) Alignment usage (turn)

Source: Internet
Author: User

What is alignment, and why you want to align it.

The memory space in modern computers is divided by byte, theoretically, it seems that access to any type of variable can start at any address, but the reality is that access to a particular variable is often at a specific memory address, which requires that all types of data be arranged in space according to certain rules, Instead of sequencing one after another, that's the alignment.
The function and reason of the alignment: each hardware platform has a great difference in the processing of storage space. Some platforms have access to certain types of data only from certain addresses. Other platforms may not be able to do this, but the most common is the loss of access efficiency if the data is not aligned according to its platform requirements. For example, some platforms start every time from the even address, if an int (assuming 32-bit system) if stored at the beginning of the even address, then a read cycle can be read out, and if stored in the beginning of the odd address, it may require 2 read cycles, The int data can be obtained by piecing together the high and low byte of the result of two times. Obviously, the reading efficiency is much lower. This is also the game of space and time.

The implementation of the alignment
Usually, when we write a program, we don't need to consider the alignment problem. The compiler will select the alignment policy for the target platform for us. Of course, we can also notify the compiler to pass the precompiled instructions and change the alignment of the specified data. However, because we don't generally need to care about this problem, because the editor is aligned with data storage, and we don't understand it, it's often confusing to some questions. The most common is the struct data structure of the sizeof results, unexpected. To do this, we need to understand the alignment algorithm.

Usage instructions

Packs are aligned with members of struct, union, and class to specify byte boundaries. Unlike the/zp switch of the Compile Options (property-> Configuration Properties-> C + +-> code generation-> struct Member alignment), it is not for the entire project, but for modules only, such as a compilation unit.

Syntax Specific analysis:
1, #pragma pack (show)

Displays the number of bytes in the current packing aligment, displayed in the form of a warning message;

2, #pragma pack (n)

Specifies the value of the packing in bytes, the default value is 8, and the legal values are 1, 2, 4, 8, 16.

3, #pragma pack ()
Sets the current byte alignment value to the default value (usually 8).

4. #pragma pack (push)
Presses the current specified packing alignment value into the stack, where the stack is the internal compiler stack, setting the current packing alignment to n; if n is not specified, the current packing alignment numerical pressure stack;

5, #pragma pack (POP)

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 value If identifier is specified, the record in the internal compiler stack will be found by pop until identifier, 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;

6, #pragma pack (push, N)
First press the current byte alignment to the top of the compilation stack, and then set N to the current value.

7, #pragma pack (pop, N)
POPs the byte alignment value at the top of the stack stack, discards it, and then sets N to the current value.

8. #pragma pack (push, identifier)
Presses the current byte alignment value onto the top of the compilation stack, and then identifies the position where the value is stored in the stack as identifier.
9. #pragma pack (pop, identifier)
POPs the value identified as the identifier location in the compilation stack and sets it to the current value. Note that if there is a value above the position identified in the stack, it will be ejected and discarded first.
#pragma pack (push, identifier, n)
Press the current byte alignment to the top of the compilation stack, and then identify the location where the value is stored in the stack as identifier, and then set N to the current value.
#pragma pack (pop, identifier, n)
POPs the value identified as the identifier location in the compilation stack, discards it, and then sets N to the current value. Note that if there is a value above the position identified in the stack, it will be ejected and discarded first.

(identifier: optional parameter; When used with a push, gives 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, ignore pop if identifier not found;

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 overall alignment < attention of complex types (such as structs) is that the "whole" > is performed according to the smaller value between the data member with the largest length in the structure and the specified 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;

6, structure, or the self alignment value of a class: the value of its own alignment value in its data member.

7. Valid alignment values for data members, structs, and classes: their own alignment values and the value of the specified alignment value.

application Example:

#include 
struct a 
{ 
     int      A; 
     Char    b; 
     Short C; 
}; 


 int main ()
{
    cout << sizeof (A)

Show sizeof (A) as 8;

Analysis: A's alignment length is 4 bytes, holds the address: 0x0000 ~ 0x0003;

B's alignment length is 1 bytes, store address: 0x0004

C of the alignment length of 2 bytes, storage address: 0x0006 ~ 0x0007;

Position A and B transformation

#include 
struct A 
{ 
     char    b; 
     int      A; 
     Short C; 
}; 


 int main ()
{
    cout << sizeof (A)

Show sizeof (A) as 12;

Analysis:

B's alignment length is 1 bytes, store address: 0x0000

A's alignment length is 4 bytes, store address: 0x0004 ~ 0x0007;

C of the alignment length of 2 bytes, storage address: 0x0008 ~ 0x0009;

A's alignment is 4, so sizeof (a) is an integer multiple of 4:12

#include 


#pragma pack (push)//Save alignment Status
#pragma pack (2)

struct A 
{ 
     char    b; 
     int      A; 
     Short C; 
}; 
#pragma pack (POP)/recovery alignment Status

 int main ()
{

    cout << sizeof (A)
Analysis:
The alignment length specified here is 2;

B's alignment length is 1 bytes, store address: 0x0000

The alignment length of a is 2 bytes, store address: 0x0002 ~ 0x0005;//Valid alignment values for data members, structs, and classes: self-aligning values and the value of the specified alignment value.

C of the alignment length of 2 bytes, storage address: 0x0006 ~ 0x0007;

A's alignment is 2, (here 8 is a multiple of 2, so you don't have to fill 0)

So sizeof (A) is 2 integer times: 8

#include 


#pragma pack (push)//Save alignment Status
#pragma pack (1)

struct A 
{ 
     char    b; 
     int      A; 
     Short C; 
}; 
#pragma pack (POP)/recovery alignment Status

 int main ()
{

    cout << sizeof (A)
Analysis:
The alignment length specified here is 1;

B's alignment length is 1 bytes, store address: 0x0000

A's alignment length is 1 bytes, store address: 0x0001 ~ 0x0004;

C of the alignment length of 1 bytes, storage address: 0x0005 ~ 0x0006;

A's alignment is 1, (here 7 is a multiple of 1, so you don't have to fill 0)

So sizeof (A) is 4 integer times: 7

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.