I'm working with bytes in C++ and was wondering about byte order in memory while taking padding between struct members into consideration, and whether endianness of the system affects the byte order in memory of that struct.
Let's say I have struct:
struct MyStruct {
unsigned char a;
int b;
};
Member a takes up 4 bytes (padding was added) and member b also takes up 4 bytes. The question is: Does endianness affect a's byte order in memory or not?
Let's assume that a = 0x01 and b = 0x00000000; will the byte order be:
0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
... for both big-endian and little-endian systems, or can it be: or
0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00
Perhaps there's a misunderstanding here. The member
ais of typeunsigned char, so it always takes up exactly on byte of memory, becausesizeof(unsigned char) == 1.However, it is possible that the compiler inserts padding bytes between
structmembers. These padding bytes don't belong to any member, but to thestructitself. This is mostly done to ensure correct alignment of subsequent members. What will likely happen in your example:No matter how many padding bytes there are,
aalways appears at the beginning of thestruct, so the only legal way to lay out this memory is:It's irrelevant whether the platform endianness is little-endian or big-endian when it comes to the position of members. The endianness only specifies how the bytes inside of an object are laid out. The padding between
structmembers has nothing to do with that, and is the result of object size and alignment.See also: Is a struct's address the same as its first member's address?
Note: You can use non-standard attributes such as
[[gnu::packed]]to eliminate padding bytes betweenstructmembers, if really necessary.