#pragma pack
Go Up to Pragma Directives Overview Index
Contents
- 1 Syntax (See Pseudo-grammar)
- 2 Description
- 2.1 #pragma pack with no arguments
- 2.2 #pragma pack using a value for n
- 2.3 #pragma pack using push
- 2.4 #pragma pack using push, identifier
- 2.5 #pragma pack using push and n
- 2.6 #pragma pack using pop
- 2.7 #pragma pack using pop, identifier
- 2.8 #pragma pack using pop and n
- 2.9 Error conditions
- 2.10 Warning conditions
Syntax (See Pseudo-grammar)
#pragma pack([{push | pop}[,]] [identifier[,]] [n])
Description
The #pragma pack(n)
directive is the same as using the #pragma option
specifically with the -a
compiler option. n
is the byte alignment that determines how the compiler aligns data in stored memory. For more information, see the -a
compiler option. #pragma pack
can also be used with push and pop arguments, which provide the same functionality as the #pragma option
directive using push and pop.
The following table compares the use of #pragma pack
with #pragma option
:
#pragma pack | #pragma option |
---|---|
#pragma pack(n) |
#pragma option -an |
#pragma pack(push, n) |
#pragma option push -an |
#pragma pack(pop) |
#pragma option pop |
The #pragma pack
directive also supports an identifier argument, which must be used in combination with either push or pop.
#pragma pack with no arguments
#pragma pack()
Using #pragma pack
with no arguments will set the packing size to the starting -aX alignment (which defaults to 8). The starting -aX alignment is considered the alignment at the start of the compile AFTER all command-line options have been processed.
#pragma pack using a value for n
#pragma pack(8)
Using #pragma pack
with a value for n will set the current alignment to n. Valid alignments for n are: 1, 2, 4, 8, and 16.
#pragma pack using push
#pragma pack(push)
Using #pragma pack
with push will push the current alignment on an internal stack of alignments.
#pragma pack using push, identifier
#pragma pack(push, ident)
Using #pragma pack
with push and an identifier will associate the pushed alignment with 'identifier'.
#pragma pack using push and n
#pragma pack(push, 8) #pragma pack(push, ident, 8)
Using #pragma pack
with push with a value for 'n', will execute pragma pack push or pragma pack push identifier, and afterwards set the current alignment to 'n'.
#pragma pack using pop
#pragma pack(pop)
Using #pragma pack
with pop will pop the alignment stack and set the alignment to the last alignment pushed. If the pop does not find a corresponding push, the entire stack of alignments is popped, and a warning is issued, and the alignment reverts to the starting -aX alignment.
#pragma pack using pop, identifier
#pragma pop(pop, ident)
Using #pragma pack
with pop and an identifier will pop the stack until the identifier is found and set the alignment to the alignment pushed by the previous corresponding #pragma pack(push, identifier)
. If the pop with identifier does not find the corresponding push with an identifier, the entire stack of alignments is popped, and a warning is issued to the user:
W8083 Pragma pack pop with no matching pack push
The alignment will then be reset to the starting -aX alignment.
#pragma pack using pop and n
#pragma pack(pop, 8) #pragma pack(pop, ident, 8)
Using #pragma pack
with pop and a value for 'n' will execute pragma pack pop or pragma pack pop identifier. Afterwards, the current alignment is set to 'n', unless pop fails to find a corresponding push, in which case 'n' is ignored, a warning is issued, and the alignment reverts to the starting -aX alignment.
Error conditions
Specifying an 'identifier' without push or pop is an error.
Specifying an alignment different from 1, 2, 4, 8, 16 is an error.
Warning conditions
Using #pragma pop
without a corresponding push issues a warning.