Core · Module 4 · Lesson 1 of 9
Structures, Alignment, and Padding
Inspect hardware memory alignment, calculate compiler struct padding and trailing bytes, and optimize composite data layouts.
By the end you can
- Calculate internal and trailing struct padding bytes introduced by the compiler
- Inspect field byte offsets using the offsetof macro from stddef.h
- Reorder struct members by descending alignment to minimize memory waste
- Evaluate the architectural costs and risks of packed structures
Builds on Function Pointers and Callback Architecture.
01 · A question first
Why does a struct containing one char, one 32-bit int, and another char consume 12 bytes of RAM instead of 6? And why does swapping the declaration order of two fields shrink the struct to 8 bytes? Understanding hardware word alignment reveals the hidden bytes compilers insert.
Make a guess before you read on.
02 · The idea
Members only
Unlock the full lesson
This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.
What's inside
- Start with the problem
- How the idea works
- Why it is correct
- What it costs
- Pitfalls and variations
- Where it is used
03 · Practice
Asked in interviews
Something wrong with this lesson? Report a problem