Structure-Layout Pragmas (Using the GNU Compiler Collection (GCC)) (original) (raw)
6.5.11 Structure-Layout Pragmas ¶
For compatibility with Microsoft Windows compilers, GCC supports a set of #pragma pack
directives that change the maximum alignment of members of structures (other than zero-width bit-fields), unions, and classes subsequently defined. The n value below specifies the new alignment in bytes and may have the value 1, 2, 4, 8, and 16. A value of 0 is also permitted and indicates the default alignment (as if no #pragma pack
were in effect) should be used.
#pragma pack(n)
Sets the new alignment according to n.
#pragma pack()
Sets the alignment to the one that was in effect when compilation started (see also command-line option-fpack-struct[=n]. See Options for Code Generation Conventions).
#pragma pack(push[,n])
Pushes the current alignment setting on an internal stack and then optionally sets the new alignment.
#pragma pack(pop)
Restores the alignment setting to the one saved at the top of the internal stack (and removes that stack entry). Note that #pragma pack([n])
does not influence this internal stack; thus it is possible to have #pragma pack(push)
followed by multiple #pragma pack(n)
instances, with the original state restored by a single #pragma pack(pop)
.
You can also use the packed
type attribute (see Common Type Attributes) to pack a structure. However, the packed
attribute interferes with #pragma pack
, and attempting to use them together may cause spurious warnings or unexpected behavior.
Some targets, e.g. x86 and PowerPC, support the #pragma ms_struct
directive, which causes subsequent structure and union declarations to be laid out in the same way as__attribute__ ((ms_struct))
; see x86 Variable Attributes.
#pragma ms_struct on
Turns on the Microsoft layout.
#pragma ms_struct off
Turns off the Microsoft layout.
#pragma ms_struct reset
Goes back to the default layout.
Most targets also support the #pragma scalar_storage_order
directive which lays out subsequent structure and union declarations in in the same way as the documented__attribute__ ((scalar_storage_order))
; see Common Type Attributes.
#pragma scalar_storage_order big-endian
#pragma scalar_storage_order little-endian
Set the storage order of scalar fields to big- or little-endian, respectively.
#pragma scalar_storage_order default
Goes back to the endianness that was in effect when compilation started (see also command-line option-fsso-struct=endianness see Options Controlling C Dialect).