Use of pack keywords

Source: Internet
Author: User

Pack

# Pragma pack ([N])

This command specifies the Compact alignment between the structure and the federated members. The structure and Union of a complete conversion unit

 
The Compact alignment is set by the/ZP option. Compact alignment is configured at the data description layer with pace compilation instructions. This compilation instruction takes effect in the first structure or joint description after it appears. This compilation instruction is invalid for the definition.
When you use # pragma
Pack (N), where N is 1, 2, 4, 8, or 16. Each structure member after the first structure member is stored in a smaller Member type or N-byte limit. If you use no parameters
# Pragma pack, the structure member is compact to the value specified by/ZP. The default/ZP compact value is/zp8.

The compiler also supports the following enhanced Syntax:

# Pragma pack ([{push | Pop},] [identifier,] [N])

If different components use pack compilation instructions to specify different compact alignment, this syntax allows you to combine program components into a separate conversion unit.

 
Each occurrence of pack compilation instructions with push parameters stores the current Compact alignment in an internal compiler stack. The compilation indicator parameter table is read from left to right. If you use push
The pooled value is stored. If you give a value of N, the value will become a new compact value. If you specify an identifier, that is, you select a name, the identifier is associated with the new compact value.



Each time a pack compilation indicator with a pop parameter appears, the value at the top of the internal compiler stack is retrieved and the value is a new compact alignment value. If you use the pop parameter and the internal compiler stack is empty
The Compact value is the value given by the command line, and a warning message is generated. If you use Pop and specify a value of N, this value will become a new compact value.

 
If you use Pop and specify an identifier, all values stored in the stack will be deleted from the stack until a matching identifier is found, this compact value related to the identifier is also removed from the stack, and this is only
The Compact value that existed before the identifier entered the stack becomes the new compact value. If no matching identifier is found, the Compact value set by the command line is used and a level-1 warning is generated. The default compact alignment is 8.

The new enhancements of pack compilation instructions allow you to write header files to ensure that the Compact values before and after this header file are the same.

/* File name: include1.h */

# Pragma pack (push, enter_include1)

/* Your include file code ...*/

# Pragma pack (POP, enter_include1)

/* Terminate de1.h */

 
In the preceding example, the current Compact value is associated with the identifier enter_include1 and is pushed into the header file. Pack compilation at the end of the header file indicates to delete all files that may appear in the header file
Compact values in the device, and delete compact values related to enter_include1. Therefore, make sure that the Compact values before and after the header file are the same.

This new feature also allows you to use code, such as header files, which can use pack compilation instructions to set compact alignment different from the Compact values set in your code:

# Pragma pack (push, before_include1)

# Include "include1.h"

# Pragma pack (POP, before_include1)

In the above example, your code is protected for any changes to the compact value that appears in include. h.

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.