RAD Studio |  |

#pragma pack

Syntax

Copy Code

    #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

Copy Code

    #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

Copy Code

    #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

Copy Code

    #pragma pack(push)

Using #pragma pack with push will push the current alignment on an internal stack of alignments.

#pragma pack using push, identifier

Copy Code

    #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

Copy Code

    #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

Copy Code

    #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

Copy Code

    #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:

Copy Code

    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

Copy Code

    #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.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!