Project 2: Struct Layout Detective
A C program that defines several structs with different member orders and uses
sizeofandoffsetofto print a detailed report of their memory layout, including the size and location of padding bytes.
Quick Reference
| Attribute | Value |
|---|---|
| Primary Language | C |
| Alternative Languages | C++, Rust |
| Difficulty | Level 1: Beginner |
| Time Estimate | Weekend |
| Knowledge Area | Memory Layout / Data Alignment |
| Tooling | C compiler |
| Prerequisites | Basic C programming. |
What You Will Build
A C program that defines several structs with different member orders and uses sizeof and offsetof to print a detailed report of their memory layout, including the size and location of padding bytes.
Why It Matters
This project builds core skills that appear repeatedly in real-world systems and tooling.
Core Challenges
- Predicting
sizeof→ maps to understanding thatsizeof(struct)is not just the sum ofsizeof(member) - Calculating padding → maps to manually applying alignment rules
- Observing architecture differences → maps to running the same code on x86 and ARM shows different results
- Forcing packed layout → maps to using
__attribute__((packed))and understanding the performance trade-offs
Key Concepts
- Data Structure Alignment: Wikipedia has an excellent, detailed article on this.
offsetofmacro:stddef.hdocumentation.- Type-safe printing:
inttypes.hfor macros likePRId64to print architecture-independent types correctly.
Real-World Outcome
Analyzing 'struct Example1':
sizeof = 16 bytes
'c1' (char) at offset 0, size 1
-- padding -- at offset 1, size 7
'd1' (double) at offset 8, size 8
Analyzing 'struct Example2' (optimized order):
sizeof = 16 bytes
'd1' (double) at offset 0, size 8
'c1' (char) at offset 8, size 1
-- padding -- at offset 9, size 7 (or less if other members follow)
Conclusion: Reordering members to place larger types first can reduce padding.
Implementation Guide
- Reproduce the simplest happy-path scenario.
- Build the smallest working version of the core feature.
- Add input validation and error handling.
- Add instrumentation/logging to confirm behavior.
- Refactor into clean modules with tests.
Milestones
- Milestone 1: Minimal working program that runs end-to-end.
- Milestone 2: Correct outputs for typical inputs.
- Milestone 3: Robust handling of edge cases.
- Milestone 4: Clean structure and documented usage.
Validation Checklist
- Output matches the real-world outcome example
- Handles invalid inputs safely
- Provides clear errors and exit codes
- Repeatable results across runs
References
- Main guide:
LEARN_C_ABI_DEEP_DIVE.md - “The C Programming Language” by Kernighan & Ritchie