Is it possible to ... change the current structure alignment setting in gcc/g++?
Yes. GCC pragmas and command line options are described in GCC documentation.
There is a command line option:
-fpack-struct[=n]
Without a value specified, pack all structure members together without holes. When a value is specified (which must be a small power of two), pack structure members according to this value, representing the maximum alignment (that is, objects with default alignment requirements larger than this are output potentially unaligned at the next fitting location.
Warning: the -fpack-struct switch causes GCC to generate code that is not binary compatible with code generated without that switch. Additionally, it makes the code suboptimal. Use it to conform to a non-default application binary interface.
And pragmas:
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.
Is it possible to detect ... the current structure alignment setting in gcc/g++?
Depends a lot on what you mean by "detect". There is following command line option:
-frecord-gcc-switches
This switch causes the command line that was used to invoke the
compiler to be recorded into the object file that is being created.
This switch is only implemented on some targets and the exact
format of the recording is target and binary file format dependent,
but it usually takes the form of a section containing ASCII text.
If such option was used, then you can detect whether -fpack-struct[=n] was used by inspecting the binary.
If you simply want to know the alignment of a particular type, you can use the alignof
operator. If you want to know the maximum alignment of any scalar type, you can use alignof(std::max_align_t)
. You can specify classes or arrays to have stricter alignment than they would otherwise have with alignas
. If that exceeds the alignment of std::max_align_t
, then the class is said to be over-aligned.
__attribute__ ((aligned (x)))
so no need to detect anything. – Eugene Sh.__attribute__((packed))
(which should be banned). – EOF__attribute__((packed))
is not an adequate substitute for proper serialization. – EOFuint8_t
-array. Better yet, define a grammar for the serialized format, autogenerate the code, for both serializing and deserializing, so you don't have any mismatch, neither between serializing and deserializing, nor among different architectures. – EOF