c - Alignment of struct with explicit width members on different compilers -
i wondering, if structs manually padded each member aligned @ address multiple of size , objects of known explicit width, degree can expect binary layout "expected"/intended one? perhaps there compiler specific options ensure layout declared?
iirc c standard not allow reordering, eventual padding in question. compiler need add padding if structure padded according own alignment rules?
by default, compiler align 32 or 64bit. , yes, there compiler specific options set alignment custom value.
so if want control alignment of struct fields, can either compiler flags or pragmas.
with gcc -fpack-struct=[n]
in source code, can surround definition of struct #pagma pack
or #pagma push(pack)
:
#pragma pack(push(n)) typedef struct my_struct {...} my_struct_t; #pragma pack(pop)
gcc documentation: http://gcc.gnu.org/onlinedocs/gcc/structure-packing-pragmas.html
msdn documentation: http://msdn.microsoft.com/en-us/library/2e70t5y1.aspx
Comments
Post a Comment