0

While I understand how padding generally works in a C struct, I struggle to understand how inner struct are padded. Example:

struct inner
{
    int a;
}

struct outer
{
   char a;
   struct inner b;
}

How is the padding between member outer.a and member outer.b calculated?

How is the padding of the end of struct outer calculate when the data type of member inner.a changes from int to short or double?

What are the implications of calculating these paddings in a 32 bit architecture versus a 64 bit one?

  • See https://stackoverflow.com/questions/4306186/structure-padding-and-packing and https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member – Mickael B. Jan 23 '20 at 17:24
  • Does this answer your question? [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) – Acorn Jan 23 '20 at 17:25
  • Why not print out an instance of the struct as hex and see for yourself? – Gillespie Jan 23 '20 at 17:25
  • 1
    with the exception of very few, very special cases (e.g. when you need to access these structure members using assembler), you should avoid any construct that would require that specific knowledge. Even if you need that, you should rather let the compiler do the required decisions (using the `offsetof()` macro) instead of trying to do that yourself. – mfro Jan 23 '20 at 17:30
  • Idea is to do padding from inside out. Inner struct is aligned now only outer struct has to be padded. Like, `struct outer{struct inner b; char a; char reserve[3]}` – TruthSeeker Jan 23 '20 at 17:32
  • 1
    @TruthSeeker it seems that the members of `struct outer` are (wrongly) inverted in your code snippet. Also, what happens if member `a` of struct `inner` changes from `int` to `short` or `double`. What would be then the padding number? –  Jan 23 '20 at 17:38
  • Yes, it was wrong snippet. Thanks for notice. Every time a `struct` members are updated *ONLY* that `struct` has to be be padded. Hence overall padding will be unaffected. `outer` `struct` can assume `inner` `struct` is always padded. – TruthSeeker Jan 23 '20 at 17:49
  • It's all in the standard. Admittedly it can be hard to read, but get used to it. It will help you in the future. – the busybee Jan 23 '20 at 18:24
  • 1
    Padding is implementation-specific, which means each implementation may have different padding, which means it depends on the compiler. Each compiler may insert different (or same) padding and may calculate the padding in a different way. The C standard does not specify _how_ to calculate the padding, it just says it's implementation defined. Implementation defines how to calculate the padding. So you have to specify which compiler you have in mind, then someone can find the source code where this compiler calculates the padding. – KamilCuk Jan 23 '20 at 19:02
  • The compiler can either be Microsoft Visual Studio C or GCC C. –  Jan 23 '20 at 19:59

1 Answers1

0

I always find it better to hex-dump such things and play around in order to understand better. Here is an example for your case:

example

Ugur Ilter
  • 92
  • 3