Is the a amount of padding in C struct
well defined in standards or compiler and/or target architecture dependent. I could not find an answer in the standards.
It is implementation-defined. From section 6.7.2.1 of the C99 standard:
...Each non-bit-field member of a structure or union object is aligned in an implementation-defined manner appropriate to its type...
...There may be unnamed padding within a structure object, but not at its beginning...
The compiler will typically choose an arrangement that suits the underlying hardware (aligning things to be easy to read from memory, etc.).
Padding is determined by the implementation, by which we mean the specific compiler that you use to compile the code.
On certain platforms there are agreed conventions as to what padding will be used. This is what makes binary interop possible for code compiled by different compilers.
So, whilst in theory different compilers can make different choices, so long as all the code is running inside a single process, different padding rules are seldom an issue. On the other hand, if you decide to use struct
layouts for data that you send over a network, then you will invariably run into problems. Don't use the binary layout structs for data that goes on the wire.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With