0

So I read on a textbook that there are specific cases when the struct can change sizes when the endianness is changed. I am not sure, but I think that the example was

struct X { int a, char b };

Can someone explain if this is true ?

  • the compiler can pad the structure however it likes, its not defined by the c++ standard – Alan Birtles Feb 27 '23 at 08:15
  • The book must have meant something else, because endianness is only about byte-order for individual variables, not sets of variables. So for the structure you show, the compiler can't change the order of the members. It's also very unlikely that different endianness could lead to different padding. A 32-bit `int` value will always be a 32-bit `int` value, no matter its endianness. – Some programmer dude Feb 27 '23 at 08:15
  • See [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member) and also [Structure padding and packing](https://stackoverflow.com/questions/4306186/structure-padding-and-packing) – Jason Feb 27 '23 at 08:16
  • Which book? What page number? See [Does the endianness affect how structure members are stored into the memory](https://stackoverflow.com/questions/38992147/does-the-endianness-affect-how-structure-members-are-stored-into-the-memory) – Jason Feb 27 '23 at 08:16
  • It is likely that if systems A and B differ in endianness, A and B might also differ in other regards that impact alignment or size of various types. So that might be the real cause you see different struct sizes. – bitmask Feb 27 '23 at 08:17
  • What *can* change structure size s different target platform with different alignment requirements or different sizes of the types. It could also mean different endianness, but it's not the endianness that are the cause of the structure size being changed. – Some programmer dude Feb 27 '23 at 08:17

1 Answers1

1

A change in endianness means you are on a different system where the compiler is free to define padding, integer size and byte size which could all lead to a different struct size, but it would not be because of the endianness.

nielsen
  • 5,641
  • 10
  • 27
  • Further, even on the same system, different compilers can lay out structs differently; and even the **same** compiler can lay out structs differently (usually when you specify different command-line switches). +1. – Pete Becker Feb 27 '23 at 14:18