The C Standard, 18.104.22.168 [ISO/IEC 9899:2011], states
There may be unnamed padding within a structure object, but not at its beginning. . . . There may be unnamed padding at the end of a structure or union.
Subclause 6.7.9, paragraph 9, states that
unnamed members of objects of structure and union type do not participate in initialization. Unnamed members of structure objects have indeterminate value even after initialization.
The only exception is that padding bits are set to zero when a static or thread-local object is implicitly initialized (paragraph10):
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:
— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
Because these padding values are unspecified, attempting a byte-by-byte comparison between structures can lead to incorrect results [Summit 1995].
Noncompliant Code Example
In this noncompliant code example,
memcmp() is used to compare the contents of two structures, including any padding bytes:
In this compliant solution, all of the fields are compared manually to avoid comparing any padding bytes:
EXP42-C-EX1: A structure can be defined such that the members are aligned properly or the structure is packed using implementation-specific packing instructions. This is true only when the members' data types have no padding bits of their own and when their object representations are the same as their value representations. This frequently is not true for the
_Bool type or floating-point types and need not be true for pointers. In such cases, the compiler does not insert padding, and use of functions such as
memcmp() is acceptable.
This compliant example uses the
#pragma pack compiler extension from Microsoft Visual Studio to ensure the structure members are packed as tightly as possible:
Comparing padding bytes, when present, can lead to unexpected program behavior.
|Axivion Bauhaus Suite|
|LDRA tool suite|
|618 S||Partially implemented|
Detected by the addon cert.py
Does not warn about global/static padding data as this is probably initialized to 0
Don't memcpy or memcmp non-PODs
Checks for memory comparison of padding data (rule fully covered)
Exhaustively verified (see the compliant and the non-compliant example).
Key here (explains table format and definitions)
|ISO/IEC TS 17961||Comparison of padding data [padcomp]||Prior to 2018-01-12: CERT: Unspecified Relationship|
|CERT C||EXP62-CPP. Do not access the bits of an object representation that are not part of the object's value representation||Prior to 2018-01-12: CERT: Unspecified Relationship|
|[ISO/IEC 9899:2011]||22.214.171.124, "Structure and Union Specifiers"|
|[Summit 1995]||Question 2.8|