Is the amount of padding in structure in C is Compiler dependent or well defined?

爷,独闯天下 提交于 2019-12-13 13:28:26

问题


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.


回答1:


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.).




回答2:


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.



来源:https://stackoverflow.com/questions/10374922/is-the-amount-of-padding-in-structure-in-c-is-compiler-dependent-or-well-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!