Padding, alignment, and layout
Separate logical fields from their representation and query layout without guessing portable sizes or bit patterns.
In this chapter8 sections
A structure’s members appear in a defined order, but their sizes do not always add up to the structure’s size. Implementations may insert padding and require alignment. These rules matter when accessing objects or designing an external format.
1. Member order and possible gaps
For ordinary non-bit-field structure members, later members appear at increasing addresses in declaration order. A structure has no padding before its first member, but padding may appear between members or at the end. Tail padding helps arrays of the structure meet the relevant alignment requirements for every element.
- first member
- char mark
- possible gap
- padding
- next member
- int count
- possible tail
- padding
Reordering members can change padding and can change an exposed interface’s layout. It is not a universal rule that putting the largest member first always minimizes size. Members also have alignment requirements, and different implementations can make different permitted layout choices. Measure the target implementation when layout matters.
2. Ask the implementation for size, offset, and alignment
Include <stddef.h> for size_t and offsetof. sizeof gives the object’s complete byte size, including padding. offsetof gives an appropriate ordinary member’s offset in bytes. C17 _Alignof(type) queries a type’s alignment requirement, also expressed in bytes.
struct Sample { char mark; int count; };
size_t count_offset = offsetof(struct Sample, count);
size_t object_size = sizeof(struct Sample);
size_t required_alignment = _Alignof(struct Sample);
The code makes no claim that int occupies four bytes or that this particular structure occupies eight. Do not apply offsetof to a bit-field, and do not take a bit-field’s address. A member offset is a layout fact, not a permission to form an invalid pointer or access another type through arbitrary byte arithmetic.
3. Alignment and access type are separate requirements
Alignment constrains where suitable objects can be located. C17 _Alignas can specify alignment for an object subject to the language’s restrictions; it cannot weaken the object’s natural requirement. An extended alignment may be unsupported in a particular context. Ordinary allocation’s guarantees should not be generalized to every over-aligned type.
_Alignas(int) unsigned char storage[sizeof(int)] = {0};
size_t capacity = sizeof storage;
The declaration requests alignment suitable for an int, but the object is still a declared array of unsigned char. Alignment alone does not turn it into an int object or make arbitrary typed access valid. Keep storage capacity, alignment, lifetime, and access-type rules as separate checks.
4. Bit-fields name small integer fields, not a portable wire layout
struct Flags { unsigned int enabled : 1; unsigned int mode : 2; };
struct Flags flags = {.enabled = 1, .mode = 2};
Bit-fields declare a permitted width for an integer member. In C17, the standard base types include _Bool, signed int, and unsigned int; support for other base types and the signedness of a plain int bit-field are implementation-defined. The ordering of bits within allocation units and whether a field can straddle a unit boundary depend on the implementation; some details are unspecified rather than guaranteed. This example checks field values without predicting a byte image. Hardware register mapping also needs the platform’s ABI, access, and device rules; a bit-field declaration alone is insufficient.
5. Stable meaning needs an explicit representation policy
Padding is not a useful record field. C17 initialization and later member stores do not generally provide a canonical padding pattern for ordinary structure values. For aggregates and unions initialized with {}, C23 initializes padding to zero bits; later stores can leave padding unspecified. Other representations still matter too. Compare intended members for semantic equality; define an explicit encoding for files or messages instead of treating a raw structure dump as a portable format.
Primary references and further reading
- WG14 N1570 — public C11 committee draft: 6.2.6.1, 6.2.8, 6.7.2.1, 6.7.5, 7.19, 7.15. Stable core rules used for the C17 baseline; this is not the final ISO publication.
- WG14 N3096 — public C23 working draft: 6.7.10 and alignas/alignof keyword rules. C23 notes are separate from the C17 example checks.
Before you move on
- Alignment constrains which storage positions are suitable for a type.
- Structure padding can make sizeof exceed the sum of member sizes.
- Layout details and bit-field behavior need implementation-aware checks and explicit external-format rules.
Keep these ideas separate
- Member values / Complete representation
- Equal logical members do not require every padding byte of two objects to be equal.
Why can comparing complete structure bytes fail to express the equality your application wants?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- Yes, every structure contains only its meaningful values and no padding.
Implementations can insert padding, and representations need not provide this general guarantee.
- No. Padding and representations can make byte comparison unsuitable for value equality.
✓ Correct reasoning. Compare meaningful members according to the equality your program intends.
- memcmp always performs a member-by-member semantic comparison.
memcmp compares bytes, not the meanings of structure members.
C17
Ready for the next idea?
Mark this chapter as read whenever you feel ready.