I have a struct in C:
typedef struct {
char member_a;
char member_b;
char member_c;
char member_d;
} mystruct;
From what I understand, C structs store their members in memory contiguously. If I print out the struct's memory I can see that is the case, but it looks like the order of the members is reversed.
mystruct m;
m.member_a = 0xAA;
m.member_b = 0xBB;
m.member_c = 0xCC;
m.member_d = 0xDD;
printf("%X\n", m);
This outputs:
DDCCBBAA
Is this because the struct's member's values are stored in memory in reverse order?
So the memory would look something like this, if m
was stored at memory location 0x00 and each location was 1 byte in size:
memory location | value |
---|---|
0x00 | 0xDD |
0x01 | 0xCC |
0x02 | 0xBB |
0x03 | 0xAA |
is this always the case with C? is this compiler specific? architecture specific? other?
Using gcc on Mac
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin