Struct Memory Lab
See exactly where every field lands: offsets, alignment, padding, and total size — on three different targets.
Ground rule: exact struct layout depends on the compiler, ABI, architecture, packing attributes, and alignment settings. This lab shows typical layouts so you build correct intuition — verify with sizeof / offsetof on your real toolchain.
3.1 Interactive layout visualizer
🧲 Memory panel
Why reordering saves RAMchar, int, char needs 3+3 padding bytes (12 total); int, char, char needs only 2 tail bytes (8 total). With 1,000 records in a ring buffer, ordering alone saves 4 KB — real money on a 64 KB MCU. Rule of thumb: largest fields first.
Why packed structs bite back
Packing removes padding but creates unaligned fields. Some cores fault on unaligned access; others quietly split it into slow multi-byte reads. Packed ≠ portable.
Protocol trap: casting a received byte buffer straight to a struct couples your code to this compiler’s padding and this CPU’s endianness. Parse protocol data with explicit shifts/memcpy.
3.2 Memory Bug Toggle
Same struct, six ways to touch it. Switch modes and watch what the memory does.
🐞 Access mode
Why embedded failures feel spooky: there is no OS to catch a bad write. Corruption lands wherever the pointer points — so systems fail silently, reset via watchdog, corrupt unrelated data, or misbehave only at certain temperatures or timings.