Description
The #pragma pack directive specifies the alignment rules to use for the structures, unions, and classes that follow it. The packing value at the left brace of a declaration is the one that affects that declaration, regardless of whether further pack directives occur within the declaration.
Syntax
where:
| Option | Description |
| push | Stores the current boundary alignment value on the
alignment stack. If #pragma pack(push) appears first in a module, packing is performed along the boundary specified by the gen(pack, number) compiler option, or the system default alignment of 8 bytes if the gen(pack, number) option is not set. |
| pop | Removes the current boundary alignment value from the
alignment stack and makes the new value the new
alignment. Using pop on an empty stack causes a warning to be issued that the current alignment may change. Packing is performed along the boundary specified by the alignment value, if it has been specified. If not, packing is peformed along the boundary specified by the gen(pack, number) compiler option, or
if the gen(pack, number) option is not set. |
| identifier | Associates a name with the new boundary alignment
value. This value can be pushed or popped from the
alignment stack. Using push with an identifier associates the identifier with a new alignment value on the stack. If no matching identifer is on the stack, a warning is issued that the current alignment may change. The stack is not affected. Packing is performed along the boundary specified by the alignment value, if it has been specified. If not, packing is performed along the boundary specified by the gen(pack, number) compiler option, or the system default alignment of 8 bytes if the gen(pack, number) compiler option is not set. If a different identifier is not on the stack, a warning is issued that the current alignment may change. The stack is not affected. Packing is performed along the boundary specified by the alignment |
| alignment | Is boundary alignment for packing in bytes. The
alignment can be one of 1, 2, 4, 8, or 16 bytes. If no alignment is specified, packing is performed along the boundary specified by the gen(pack, number) compiler option, or
if the gen(pack, number) option is not set. Using push with an alignment value causes that value to be the new alignment. Using pop with an alignment value pops a value off the stack and sets the current alignment to alignment. |
The #pragma pack directive causes all structures, unions, and classes that follow it in the program to be packed along a boundary specified in the directive, until another #pragma pack directive changes the packing boundary.
If the data types are by default packed along boundaries smaller than those specified by #pragma pack, they are still aligned along the smaller boundaries. For example, type char is always aligned along a 1-byte boundary, regardless of the value of #pragma pack.
If more than one #pragma pack directive appears in a structure defined in an inlined function, the #pragma pack directive effective at the beginning of the definition of the structure takes precedence.
The following table summarizes how each data type is packed for the #pragma pack alignment values:
| Data Type | #pragma pack Alignment Value | |||||
|---|---|---|---|---|---|---|
| size | 1 | 2 | 4 | 8 | 16 | |
| char | 1 | 1 | 1 | 1 | 1 | 1 |
| wchar_t | 2 | 1 | 2 | 2 | 2 | 2 |
| short | 2 | 1 | 2 | 2 | 2 | 2 |
| int long | 4 | 1 | 2 | 4 | 4 | 4 |
| 8 | 1 | 2 | 4 | 4 | 4 | |
| 8 | 1 | 2 | 4 | 8 | 8 | |
| float | 4 | 1 | 2 | 4 | 4 | 4 |
| 8 | 1 | 2 | 4 | 4 | 4 | |
| 8 | 1 | 2 | 4 | 8 | 8 | |
| 16 | 1 | 2 | 4 | 4 | 4 | |
| 16 | 1 | 2 | 4 | 8 | 8 | |
| pointer | 4 | 1 | 2 | 4 | 4 | 4 |
For example,
#pragma pack(1) // this structure is packed along
// 1-byte boundaries
struct person {
char initial;
int age;
}
#pragma pack(2) // this structure is packed along
// 2-byte boundaries
struct animal {
int num_of_fingers;
float weight;
float height;
}
#pragma pack(pop) // this brings pack back to 1-byte
// boundaries
![]()
Preprocessor Directives - An
Overview
Preprocessor Directive -
#pragma
![]()
![]()
pack code generation
option - gen(pack, number)
