The Hidden Cost of Misalignment
This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).
Using `#pragma pack(1)` on structs is common in embedded C for portable binary formats, but it forces the compiler to byte-decompose every field access — even perfectly aligned ones — resulting in up to 7x more instructions on RISC-V, ARM Cortex-M0, and Xtensa. The fix is combining `__attribute__((packed, aligned(4)))`: this preserves the no-padding layout while telling the compiler the struct base pointer is always aligned, enabling native single-instruction stores for aligned fields. The post also covers struct evolution risks (new fields silently landing at misaligned offsets), binary compatibility concerns with arrays of packed structs, and how to catch alignment regressions in CI using the `struct-lint` tool (DWARF-based) or `pahole`.
Table of contents
Table of ContentsA Sensor RecordWhat the Compiler Actually EmitsThe Fix: packed + alignedStruct EvolutionNot Just RISC-VConclusionReferencesSort: