How does memory alignment affect CPU cache performance? What is structure padding?
Last updated
Alignment places data at addresses that are multiples of its natural size. Aligned data never straddles two cache lines (typically 64 bytes), while misaligned access may need two loads or even fault.
Structure padding is unused bytes the compiler inserts so each member meets its alignment. The total size is also rounded up to the largest member's alignment so arrays stay aligned.
Ordering members largest-to-smallest minimizes padding, packing more objects per cache line. Use
alignof/alignasto inspect or force alignment (e.g. to avoid false sharing).
- https://www.interviewbit.com/c-interview-questions-2026/
- https://www.interviewbit.com/c-interview-questions/
- https://dev.to/pvsdev/silent-foe-or-quiet-ally-brief-guide-to-alignment-in-c-86e
- https://dev.to/zem_code/dod-principles-in-c-part-1-struct-optimization-1anm
- https://dev.to/rafacalderon/your-code-is-slow-because-you-think-in-objects-not-data-4mn8
- https://dev.to/yanev/optimizing-memory-usage-in-go-mastering-data-structure-alignment-4beb
- https://ryonaldteofilo.medium.com/cache-line-alignment-in-c-1aac85e4482f
- https://dev.to/ali_algmass/multi-dimensional-arrays-row-major-order-a-deep-dive-3b2e