YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Designing dataChapter 35 of 73

Padding, alignment, and layout

Separate logical fields from their representation and query layout without guessing portable sizes or bit patterns.

5 min readCore conceptsC17 baseline
Offline reading
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.

A possible layout pattern
first member
char mark
possible gap
padding
next member
int count
possible tail
padding
Illustrative order only: these boxes are not measured bytes, and a gap is not required in every implementation.

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.

C17 · illustrative fragment
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.

C17 · illustrative fragment
_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

C17 · illustrative fragment
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

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.
Explain it in your own words

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.

Do equal meaningful structure-member values guarantee identical object bytes?
Read the explanation for every choice
  1. Yes, every structure contains only its meaningful values and no padding.

    Implementations can insert padding, and representations need not provide this general guarantee.

  2. No. Padding and representations can make byte comparison unsuitable for value equality.

    ✓ Correct reasoning. Compare meaningful members according to the equality your program intends.

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

CHAPTER 35 / 73

In this chapter

Padding, alignment, and layout

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping