I have a C# background. I am very much a newbie to a low-level language like C.
In C#, struct
's memory is laid out by the compiler by default. The compiler can re-order data fields or pad additional bits between fields implicitly. So, I had to specify some special attribute to override this behavior for exact layout.
AFAIK, C does not reorder or align memory layout of a struct
by default. However, I heard there's a little exception that is very hard to find.
What is C's memory layout behavior? What should be re-ordered/aligned and not?
This question is related to
c
struct
memory-layout
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.
It's implementation-specific, but in practice the rule (in the absence of #pragma pack
or the like) is:
sizeof(T)
bytes.So, given the following struct:
struct ST
{
char ch1;
short s;
char ch2;
long long ll;
int i;
};
ch1
is at offset 0s
at offset 2ch2
is at offset 4, immediately after sll
at offset 8i
is at offset 16, right after llSo sizeof(ST)
is 24.
It can be reduced to 16 bytes by rearranging the members to avoid padding:
struct ST
{
long long ll; // @ 0
int i; // @ 8
short s; // @ 12
char ch1; // @ 14
char ch2; // @ 15
} ST;
Source: Stackoverflow.com