Pairing of C/C + + development compiler instruction #pragma pack using __c++

Source: Internet
Author: User
Tags pack
C + + compiler instruction #pragma pack pairing use

#pragma pack can be used to specify the value of the memory alignment of a member variable of a C + + data structure (optional value is 1,2,4,8,16).

This article focuses on the use of pack instructions in your header file to pair them to avoid accidentally affecting the alignment of the structure members of other source files in your project.

If the structure member memory alignment of other source files is affected, then you calculate the memory size of those struct members by default alignment

Or when you use the pointer to move to compute the location of the structure member offset, you may receive unexpected exceptions.

The main possible exceptions are memory location errors or illegal memory access, which can result in incorrect positioning or numeric values, which in extreme cases can cause the program to crash.

The following examples are used to show how basic pairing is used.

1) #pragma pack (n) matching use

 

Filename:header1.h

#pragma pack (1)//memory alignment set to 1 bytes

struct s1
{
int i;
char c;
bool F;
}

    //struct s2{...}  //...

#pragma pack ()   //restore default memory alignment (paired with instructions at the beginning of the file) 

2) #pragma pack (push|pop,n) matching use

Filename:header2.h

#pragma pack (push,1)//memory alignment set to 1 bytes

struct s3
{
int i;
char c;
bool F;
}

    //struct s4{...}  //...

#pragma pack (POP)   //restore default memory alignment (paired with instructions at the beginning of the file) 

The VC++2013 Help documentation on MSDN for instructions on pack instructions is provided below for reference.

--------------------The following excerpt from the MSDN---------------------------- #pragma pack ( [n] )

Specifies packing alignment for structure and union. Whereas the packing alignment of structures and unions are set for a entire translation unit by THE/ZP option, the Packin G Alignment is set at the data-declaration level by the pack pragma. The pragma takes effect at the structure or union declaration after the pragma is seen; The pragma has no effect on definitions.

When to use #pragma pack (n), where n are 1, 2, 4, 8, or, each structure Red on the smaller member type or n-byte boundaries. If use#pragma pack without a argument, structure members are packed to the value specified BY/ZP. The Default/zp packing size IS/ZP8.

The compiler also supports the following enhanced syntax:

#pragma pack ( [[{ Push | Pop} , ] [identifier, ]] [n] )

This syntax allows your to combine program components in a single translation unit if the different components use PA CK pragmas To specify different packing alignments.

Each occurrence's a pack pragma with a push argument stores the current packing alignment on a interna l Compiler stack. The pragma ' s argument list is read from left to right. If you are push, the current packing value is stored. If you provide a value of n, that value becomes the new packing value. If you are specify an identifier, a name of your choosing, the identifier are associated with the new packing value.

Each occurrence of aPackpragma with aPopArgument retrieves the value at the top of a internal compiler stack and makes that value the new packing. If usePopand the internal compiler stack is empty, the alignment value is this set from the command-line and a warning is issued. If usePopand specify a value for n, which value becomes the new packing value. If usePopand specify an identifier, all values stored on the stack are removed to the stack until a matchingidentifier is found. The packing value associated with the identifier is also removed to the stack and the packing value that existed to be Fore the identifier was pushed becomes the new packing value. If No matching identifier is found, the packing value set from to the command line are used and a level-one warning is issued. The default packing alignment is 8.

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.