pack

C compilerC++ compiler

pack syntax

Read syntax diagramSkip visual syntax diagram#pragmapack( 124816defaultsystempopreset )

Description

The #pragma pack directive specifies the alignment rules to use for the members of the structure, union, or (C++ only) class that follows it. In C++, packing is performed on declarations or types. This is different from C, where packing is also performed on definitions.

You can also use the PACKSTRUCT option with the compiler commands to cause packing to be performed along specified boundaries. See PACKSTRUCT for more information.

Parameters

1, 2, 4, 8, 16
Structures and unions are packed along the specified byte boundaries.
default
Selects the alignment rules specified by compiler option PACKSTRUCT.
system
Selects the default IBM i alignment rules.
pop, reset
Selects the alignment rules previously in effect, and discards the current rules. This is the same as specifying #pragma pack ( ).

In the examples that follow, the words struct or union can be used in place of class.

The #pragma pack settings are stack based. All pack values are pushed onto a stack as the user's source code is parsed. The value on the top of that stack is the current packing value. When a #pragma pack (reset), #pragma pack(pop), or #pragma pack() directive is given, the top of the stack is popped and the next element in the stack becomes the new packing value. If the stack is empty, the value of the PACKSTRUCT compiler option, if specified, is used. If not specified, the default setting of NATURAL alignment is used.

The setting of the PACKSTRUCT compiler option is overridden by the #pragma pack directive, but always remains on the bottom of the stack. The keyword _Packed has the highest precedence with respect to packing options, and cannot be overridden by the #pragma pack directive or the PACKSTRUCT compiler option.

By default, all members use their natural alignment. Members cannot be aligned on values greater than their natural alignment. Char types can only be aligned along 1-byte boundaries. Short types can only be aligned along 1 or 2-byte boundaries, and int types can be aligned along on 1, 2, or 4-byte boundaries.

All 16-byte pointers will be aligned on a 16-byte boundary. _Packed, PACKSTRUCT, and #pragma pack cannot alter this. 8-byte teraspace pointers may have any alignment, although 8-byte alignment is preferred.