C + + compiler directive #pragma pack pairing use

Source: Internet
Author: User

#pragma pack can be used to specify memory-aligned values for member variables of a C + + data structure (optional value is 1,2,4,8,16).

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

If you affect the structure member memory alignment of other source files, then you calculate the size of those struct members that occupy memory when you follow the default alignment

Or you can expect unexpected exceptions when you use the pointer to move the calculated member offset position of the structure.

The main possible exceptions are memory-locating errors or illegal memory accesses, which can result in incorrect positioning or numerical values, which can cause the program to crash in extreme cases.

The following example shows how to use the basic pairing.

1) Pairing with #pragma pack (n)

 

 // Filename:header1.h #pragma // memory Alignment set to 1 bytes
    struct    S1 { int i;     Char c;   bool F; } //struct s2{...} // ... #pragma pack () // Restore Default memory alignment (used with instructions at the beginning of the file )  

2) pairing with #pragma pack (push|pop,n)

  // filename:header2.h #pragma // memory Alignment set to 1 bytes struct    S3 {  int  i;     Char  c;   bool  F; }    //struct s4{...}   // ... #pragma pack (POP)   // Restore Default memory alignment (used with instructions at the beginning of the file)      

The VC++2013 Help documentation on MSDN describes the use of the pack directive as follows, for reference.

--------------------the following excerpt from MSDN----------------------------

#pragma pack ([ N] )

Specifies packing alignment for structure and union members. 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 on the data-declaration level by the pack pragma. The pragma takes effect at the first structure or union declaration after the pragma is seen; The pragma has a no effect on definitions.

When you use the #pragma pack (n), where n is 1, 2, 4, 8, or structure member afte R The first is stored on the smaller member type or n-byte boundaries. If You use#pragma pack without a argument, structure members is 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-combine program is a single translation unit if the different Ack pragmas to specify different packing alignments.

Each occurrence of a pack pragma with a push argument stores the current packing alignment on an interna l Compiler stack. The pragma ' s argument list is read from left to right. If you use push, the current packing value is stored. If you provide a value of n, that value becomes the new packing value. If you 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 alignment. If You usePopand the internal compiler stack is empty, the alignment value are that set from the command-line and a warning are issued. If You usePopand specify a value forN, that value becomes the new packing value. If You usePopand specify anidentifier, all values stored on the stack is removed from the stack until a matchingidentifieris found. The packing value associated with theidentifieris also removed from the stack and the packing value that existed just before theidentifierWas pushed becomes the new packing value. If No matchingidentifieris found, the packing value set from the command line are used and a level-one warning is issued. The default packing alignment is 8.

C + + compiler directive #pragma pack pairing use

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.