You can start by reading the data structure alignment wikipedia article to get a better understanding of data alignment.
From the wikipedia article:
Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.
From 6.54.8 Structure-Packing Pragmas of the GCC documentation:
For compatibility with Microsoft Windows compilers, GCC supports a set of #pragma directives which change the maximum alignment of members of structures (other than zero-width bitfields), unions, and classes subsequently defined. The n value below always is required to be a small power of two and specifies the new alignment in bytes.
#pragma pack(n)
simply sets the new alignment.#pragma pack()
sets the alignment to the one that was in effect when compilation started (see also command line option -fpack-struct[=] see Code Gen Options).#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 and finalized by a single#pragma pack(pop)
.Some targets, e.g. i386 and powerpc, support the ms_struct
#pragma
which lays out a structure as the documented__attribute__ ((ms_struct))
.
#pragma ms_struct on
turns on the layout for structures declared.#pragma ms_struct off
turns off the layout for structures declared.#pragma ms_struct reset
goes back to the default layout.