#pragma pack

Source: Internet
Author: User

Original link: http://www.cnblogs.com/s7vens/archive/2012/03/06/2382236.html

Pack aligns the specified byte boundaries to the members of a struct, union, class, and so on. Unlike the/ZP switch for compilation options, it is not for the entire project, but for modules only, such as a compilation unit.

1. #pragma pack (show)
Displays the current byte-aligned value in the form of a warning message.
2. #pragma pack (n)
Sets the current byte alignment value to N.
3. #pragma pack ()
Sets the current byte alignment value to the default value (usually 8).
4. #pragma pack (push)
presses the current byte alignment value into the top of the compilation stack.
5. #pragma pack (POP)
pops the byte alignment value at the top of the compilation stack and sets it to the current value.
6. #pragma pack (push, N)
Press the current byte alignment value into 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 compilation stack, discards it, and then sets N to the current value.
8. #pragma pack (push, identifier)
presses the current byte alignment value into the top of the compiled stack, and then identifies the position where the value is saved 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 location identified in the stack, it will be ejected and discarded.
Ten. #pragma pack (push, identifier, n)
Press the current byte alignment value into the top of the compilation stack, then identify the position where the value is saved in the stack as identifier, and then set N to the current value.
One . #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.
   
Note: If the identifier in the pop is not found in the stack, the compiler ignores the instruction and does not eject any values.
   
//Code Snippet 1: The Order in which the compilation stack pops up is reversed in the order in which it is pressed
#pragma pack (show)//8 (default)
The #pragma pack (push, 16)///default 8 presses the top of the compilation stack and sets the current alignment value to.
#pragma pack (show)//above sentence set
The #pragma pack (push, 4)//16 presses the top of the compilation stack and sets the current alignment value to 4.
#pragma pack (show)//4 of the above sentence set
The #pragma pack (push, 2)//4 presses the top of the compilation stack and sets the current alignment value to 2.
#pragma pack (show)//2 of the above sentence set
The #pragma pack (push, 1)//2 presses the top of the compilation stack and sets the current alignment value to 1.
#pragma pack (show)//1 of the above sentence set
#pragma pack (POP)//pops up 2 of the top of the compilation stack and sets it to the current alignment value.
#pragma pack (show)//2
#pragma pack (POP)//pops up 4 of the top of the compilation stack and sets it to the current alignment value.
#pragma pack (show)//4
#pragma pack (POP)//pops up 16 of the top of the compilation stack and sets it to the current alignment value.
#pragma pack (show)//
#pragma pack (POP)//pops up 8 of the top of the compilation stack and sets it to the current alignment value.
#pragma pack (show)//8
 
//code snippet 2:pop with parameter n, the current byte alignment value is set to n instead of the value that was pushed in before ejecting from the top of the stack.
#pragma pack (show)//8 (default)
The #pragma pack (push, 16)///default 8 presses the top of the compilation stack and sets the current alignment value to.
#pragma pack (show)//
The #pragma pack (push, 4)//16 presses the top of the compilation stack and sets the current alignment value to 4.
#pragma pack (show)//4
The #pragma pack (push, 2)//4 presses the top of the compilation stack and sets the current alignment value to 2.
#pragma pack (show)//2
The #pragma pack (push, 1)//2 presses the top of the compilation stack and sets the current alignment value to 1.
#pragma pack (show)//1
#pragma pack (pop, 8)//Eject the top 2 of the compilation stack, discard it, and then set the current alignment value to 8.
#pragma pack (show)//8
#pragma pack (pop, 1)//eject the top 4 of the compilation stack, discard it, and then set the current alignment value to 1.
#pragma pack (show)//1
#pragma pack (pop, 2)//eject the Top 16 of the compilation stack, discard it, and then set the current alignment value to 2.
#pragma pack (show)//2
#pragma pack (pop, 16)//eject the top 8 of the compilation stack, discard it, and then set the current alignment value to.
#pragma pack (show)//
 
//code snippet 3:push and pop can have an identifier that pops up the specified value. However, those values above the specified value on the stack are ejected and discarded.
#pragma pack (show)//8 (default)
The #pragma pack (push, identifier_1, 1)//default 8 presses the top of the compiled stack and identifies the position of 8 in the stack with identifier_1, and then sets the current alignment value to 1.
#pragma pack (show)//1
#pragma pack (push, identifier_2, 2)//1 on the top of the compilation stack, and the position of 1 in the stack is identified with identifier_2, and the current alignment value is set to 2.
#pragma pack (show)//2
#pragma pack (push, Identifier_3, 4)//2 on the top of the compilation stack, and the position of 2 in the stack is identified with identifier_3, and the current alignment value is set to 4.
#pragma pack (show)//4
#pragma pack (push, Identifier_4, 8)//4 on the top of the compilation stack, and the position of 4 in the stack is identified with Identifier_4, and the current alignment value is set to 8.
#pragma pack (show)//8
#pragma pack (push, Identifier_5, 16)//8 on the top of the compilation stack, and the position of 8 in the stack is identified with the identifier_5, then the current alignment value is set to.
#pragma pack (show)//
#pragma pack (push, identifier_6)//16 on the top of the compilation stack, and 16 of the position in the stack is identified with identifier_6.
#pragma pack (show)//
#pragma pack (pop, identifier_6)//Identifier_6 The value 16 of the corresponding stack in the identifier, and set it to the current alignment value.
#pragma pack (show)//
#pragma pack (pop, identifier_5, 2)//Identifier_6 the value of the corresponding stack in the identifier, then discard it, then set the current alignment value to 2.
#pragma pack (show)//2
#pragma pack (pop, identifier_1)//pops up in the stack order until the identifier Identifier_1 8 is encountered.
#pragma pack (show)//8

#pragma pack

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.