Project 2: Struct Layout Detective

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.

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 that sizeof(struct) is not just the sum of sizeof(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.
  • offsetof macro: stddef.h documentation.
  • Type-safe printing: inttypes.h for macros like PRId64 to 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

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. 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