Project 2: Type System Explorer

An interactive program that visualizes type sizes, alignments, struct padding, and type representations in memory.

Quick Reference

Attribute Value
Primary Language C
Alternative Languages None
Difficulty Level 2 - Intermediate
Time Estimate See main guide
Knowledge Area Type Systems, Memory Layout
Tooling GCC, GDB
Prerequisites See main guide

What You Will Build

An interactive program that visualizes type sizes, alignments, struct padding, and type representations in memory.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Discovering alignment rules → Maps to struct layout optimization
  • Visualizing padding → Maps to memory efficiency
  • Understanding type qualifiers → Maps to const correctness and volatile semantics

Key Concepts

  • Map the project to core concepts before you code.

Real-World Outcome

# 1. Run type explorer
$ ./type_explorer

=== FUNDAMENTAL TYPES ===
Type            Size    Align   Signed  Min                  Max
-----------     ----    -----   ------  ---                  ---
_Bool           1       1       no      0                    1
char            1       1       impl    -128                 127
unsigned char   1       1       no      0                    255
short           2       2       yes     -32768               32767
int             4       4       yes     -2147483648          2147483647
long            8       8       yes     -9223372036854775808 9223372036854775807
float           4       4       n/a     1.175494e-38         3.402823e+38
double          8       8       n/a     2.225074e-308        1.797693e+308
void*           8       8       n/a     (pointer)            (pointer)

=== STRUCT LAYOUT ANALYSIS ===
struct example { char a; int b; char c; };

Offset  Size  Member
------  ----  ------
0       1     char a
1-3     3     [PADDING - 3 bytes wasted]
4       4     int b
8       1     char c
9-11    3     [PADDING - 3 bytes for alignment]

Total size: 12 bytes
Optimal reordering: { int b; char a; char c; } = 8 bytes (33% smaller)

# 2. Demonstrate type punning
$ ./type_explorer --punning
Float 3.14159 as bytes: 0xD0 0x0F 0x49 0x40
Float 3.14159 as int (via union): 0x40490FD0
WARNING: Type punning via pointer cast is undefined behavior!

# 3. Show type qualifiers effect
$ ./type_explorer --qualifiers
const int x = 5;
Attempting modification... COMPILER ERROR (as expected)

volatile int counter;
Assembly shows: load/store on every access (no caching in register)

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: PROFESSIONAL_C_PROGRAMMING_MASTERY.md
  • Effective C, 2nd Edition by Robert C. Seacord