C ++ compilation command # Use The pragma pack pair,

Source: Internet
Author: User

C ++ compilation command # Use The pragma pack pair,

# Pragma pack can be used to specify the memory alignment value of the member variables of the C ++ data structure (optional values: 1, 2, 8, 16 ).

This article focuses on pairing pack commands in your header files to avoid unexpected impact on memory alignment of structure members of other source files in the project.

If the memory alignment of the structure members of other source files is affected, you can calculate the memory size occupied by the structure members according to the default pair.

Or when the pointer is used to move the calculated structure member offset, unexpected exceptions may occur.

The main possible exception is the memory location error or illegal memory access. The result may lead to incorrect location or value. In extreme cases, the program may crash.

 

The following example shows how to use pairing.

1) # Use pragma pack (n) pairs

 

// Filename: header1.h # pragma pack (1) // set the memory alignment to 1 byte
Struct s1 {int I; char c; bool f ;}// struct s2 {...} //... # pragma pack () // restore the default memory alignment (paired with commands starting with the file)

 

 

 

2) # Use pragma pack (push | pop, n) pairs

// Filename: header2.h # pragma pack (push, 1) // The Memory alignment is set to 1 byte struct s3 {int I; char c; bool f ;} // struct s4 {...} //... # pragma pack (pop) // restore the default memory alignment (paired with commands starting with the file)

 

 

On MSDN, the help document of VC ++ 2013 describes how to use the pack command as follows.

-------------------- The following is an excerpt from MSDN ----------------------------

# Pragma pack ([ N] )

Specifies packing alignment for structure and union members. whereas the packing alignment of structures and unions is set for an entire translation unit by the/Zp option, the packing alignment is set at the data-declaration level byPackPragma. The pragma takes effect at the first structure or union declaration after the pragma is seen; the pragma has no effect on definitions.

When you use# Pragma pack (N), WhereNIs 1, 2, 4, 8, or 16, each structure member after the first is stored on the smaller member type orN-Byte boundaries. If you use# Pragma packWithout an 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 you to combine program components into a single translation unit if the different components usePackPragmas to specify different packing alignments.

Each occurrence ofPackPragma withPushArgument stores the current packing alignment on an internal compiler stack. The pragma's argument list is read from left to right. If you usePush, The current packing value is stored. If you provide a valueN, That value becomes the new packing value. If you specifyIdentifier, A name of your choosing,IdentifierIs associated with the new packing value.

Each occurrence ofPackPragma withPopArgument retrieves the value at the top of an internal compiler stack and makes that value the new packing alignment. If you usePopAnd the internal compiler stack is empty, the alignment value is that set from the command-line and a warning is issued. If you usePopAnd specify a valueN, That value becomes the new packing value. If you usePopAnd specifyIdentifier, All values stored on the stack are removed from the stack until a matchingIdentifierIs found. The packing value associated withIdentifierIs also removed from the stack and the packing value that existed just beforeIdentifierWas pushed becomes the new packing value. If no matchingIdentifierIs found, the packing value set from the command line is 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.