# Pragma pack usage

Source: Internet
Author: User

Pack is a struct, union, and class member alignment to specify the byte boundary. different from the/ZP switch of the compilation option (property> Configuration property> C/C ++> code generation> structure member alignment), it is not applicable to the entire project, it only targets modules, such as a compilation unit.
 
1. # pragma pack (show)
Displays the value of the Current byte alignment in the form of warning information.
2. # pragma pack (N)
Set the Current byte alignment value to n.
3. # pragma pack ()
Set the Current byte alignment value to the default value (usually 8 ).
4. # pragma pack (push)
Press the Current byte alignment value to the top of the compilation stack.
5. # pragma pack (POP)
Pop up the byte alignment value at the top of the stack compilation stack and set it to the current value.
6. # pragma pack (push, n)
First press the Current byte alignment value to the top of the compilation stack, and then set N to the current value.
7. # pragma pack (POP, n)
Pop up the byte alignment value at the top of the stack, discard it, and set N to the current value.
8. # pragma pack (push, identifier)
Press the Current byte alignment value to the top of the compilation stack, and mark the location where the value is saved in the stack as identifier.
9. # pragma pack (POP, identifier)
Pop up the value marked as the identifier position in the compilation stack and set it to the current value. Note that if there is a value above the Position identified in the stack, it will first pop up and be discarded.
10. # pragma pack (push, identifier, n)
Press the Current byte alignment value to the top of the compilation stack. Then, mark the location where the value is saved in the stack as identifier and set N to the current value.
11. # pragma pack (POP, identifier, n)
Pop up the value marked as the identifier position in the compilation stack, discard it, and set N to the current value. note: if there is a value above the Position identified in the stack, it will first pop up and be discarded.

Note: If the identifier in pop is not found in the stack, the compiler ignores this instruction and does not bring up any value.

// Code Segment 1: the order of the pop-up compilation stack is the opposite to that of the push stack
# Pragma pack (show) // 8 (default)
# Pragma pack (push, 16) // press the default value 8 to the top of the compilation stack and set the current alignment value to 16.
# Pragma pack (show) // The 16 set in the previous sentence
# Pragma pack (push, 4) // press the last sentence 16 to the top of the compilation stack and set the current alignment value to 4.
# Pragma pack (show) // 4 set in the previous sentence
# Pragma pack (push, 2) // press the last sentence 4 to the top of the compilation stack and set the current alignment value to 2.
# Pragma pack (show) // 2 set in the previous sentence
# Pragma pack (push, 1) // press the previous sentence 2 to the top of the compilation stack and set the current alignment value to 1.
# Pragma pack (show) // set 1 in the previous sentence
# Pragma pack (POP) // pop up 2 at the top of the compilation stack and set it to the current alignment value.
# Pragma pack (show) // 2
# Pragma pack (POP) // pop up the 4 at the top of the compilation stack and set it to the current alignment value.
# Pragma pack (show) // 4
# Pragma pack (POP) // The Top 16 of the compilation stack is displayed and set to the current alignment value.
# Pragma pack (show) // 16
# Pragma pack (POP) // pop up the top 8 of the compilation stack and set it to the current alignment value.
# Pragma pack (show) // 8
 
// Code Segment 2: when Pop has parameter n, the Current byte alignment value is set to N, rather than the previously pressed value popped up from the top of the stack.
# Pragma pack (show) // 8 (default)
# Pragma pack (push, 16) // press the default value 8 to the top of the compilation stack and set the current alignment value to 16.
# Pragma pack (show) // 16
# Pragma pack (push, 4) // press the last sentence 16 to the top of the compilation stack and set the current alignment value to 4.
# Pragma pack (show) // 4
# Pragma pack (push, 2) // press the last sentence 4 to the top of the compilation stack and set the current alignment value to 2.
# Pragma pack (show) // 2
# Pragma pack (push, 1) // press the previous sentence 2 to the top of the compilation stack and set the current alignment value to 1.
# Pragma pack (show) // 1
# Pragma pack (POP, 8) // pop up 2 at the top of the compilation stack, discard it, and set the current alignment value to 8.
# Pragma pack (show) // 8
# Pragma pack (POP, 1) // The Top 4 of the compilation stack is displayed, discarded, and the current alignment value is set to 1.
# Pragma pack (show) // 1
# Pragma pack (POP, 2) // pop up 16 at the top of the stack for compilation, discard it, and set the current alignment value to 2.
# Pragma pack (show) // 2
# Pragma pack (POP, 16) // the top 8 of the compilation stack is displayed, discarded, and the current alignment value is set to 16.
# Pragma pack (show) // 16
 
// Code snippet 3: Push and pop can contain identifiers, which can pop up the specified values. However, the values above the stack's specified values will be popped up and discarded.
# Pragma pack (show) // 8 (default)
# Pragma pack (push, identifier_1, 1) // press the default value of 8 to the top of the compilation stack, and mark the position of 8 in the stack with identifier_1, set the current alignment value to 1.
# Pragma pack (show) // 1
# Pragma pack (push, identifier_2, 2) // press the previous sentence 1 to the top of the stack compilation stack, and mark the position of 1 in the stack with identifier_2, set the current alignment value to 2.
# Pragma pack (show) // 2
# Pragma pack (push, identifier_3, 4) // press the previous sentence 2 to the top of the compilation stack, and mark the position of the second in the stack with identifier_3, set the current alignment value to 4.
# Pragma pack (show) // 4
# Pragma pack (push, identifier_4, 8) // press the last sentence 4 to the top of the stack compilation stack, and mark the position of 4 in the stack with identifier_4, set the current alignment value to 8.
# Pragma pack (show) // 8
# Pragma pack (push, identifier_5, 16) // press the last sentence 8 to the top of the compilation stack, and mark the position of the 8 in the stack with identifier_5, set the current alignment value to 16.
# Pragma pack (show) // 16
# Pragma pack (push, identifier_6) // press the last sentence 16 to the top of the compilation stack, and mark the position of the 16 in the stack with identifier_6.
# Pragma pack (show) // 16
# Pragma pack (POP, identifier_6) // pop up the stack value 16 corresponding to identifier_6 and set it to the current alignment value.
# Pragma pack (show) // 16
# Pragma pack (POP, identifier_5, 2) // pop up the stack value 8 corresponding to identifier_6, discard it, and set the current alignment value to 2.
# Pragma pack (show) // 2
# Pragma pack (POP, identifier_1) // click it to pop up in the order of the inbound stack. You can see 8 of the identifier_1 identifier.
# Pragma pack (show) // 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.