Aditya Sihag
Aditya Sihag

Reputation: 5167

llittle endianness of a structure in a structure

Given a hypothetical structure

struct OUTER {
  uint_16 x;
  struct INNER{
     uint_16 y;
     uint_16 z;
  } inner_struct;
} outer_struct;

and a little endian machine, how would the bytes be flipped, i.e. what would the bytes for the outer_struct look like?

Suppose x,y,z = Ox1234; Assume an alignment of 2 bytes.

I'm confused between

34 12 34 12 34 12 // x y z

and,

34 12 12 34 12 34 // x flipped-little_endian_inner_struct

Upvotes: 0

Views: 82

Answers (2)

Mark B
Mark B

Reputation: 96311

The only thing little endian flips is the order of bytes in builtin data types. The compiler is not free to re-order the attributes in your structure, and endian-ness doesn't apply to aggregate data structures (only their components). So you'll see 34 12 34 12 34 12 as the result in memory.

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129524

The order of structures members themselves do not move based on endianness in the compiler. However, you can't be certain of exactly how many bytes any particular part of your structure takes up or how many bytes of padding the compiler adds between parts of the structure. (I'm not even sure you can rely on the fact that they are in the "order of declaration", but I believe that there is something in the standard about that).

Upvotes: 0

Related Questions