Padding, Packing, and Performance: Mastering Memory Alignment in C++

Sarthak Sehgal

⏱ 60 minute session
beginner
14:00-15:00, Saturday, 20th June 2026

Have you ever wondered why your C++ class suddenly doubled in size, or why reordering its members made it smaller? This talk introduces memory alignment in C++ from the ground up - what it is, why it is critical, and how compilers handle it under the hood. We’ll start from first principles: how modern CPUs read memory, the difference between aligned and unaligned access, and the performance costs of getting it wrong. Using benchmarks, we’ll see just how impactful misalignment can be. From there, we’ll dive into how compilers lay out data members in structs and classes, and how simple reordering can reduce memory footprint. Live coding examples will illustrate how padding is introduced and how tools like alignas, alignof, and std::align give you precise control. We’ll also look at packed data structures, which trade alignment guarantees for compactness — common in trading engines, and network protocols. While they can save space, they also introduce risks like undefined behavior when misused. Through real-world examples, you'll learn when packed structures make sense — and when they’re a hazard. By the end of the talk, you'll walk away with a solid intuition for alignment and the confidence to manage memory layout effectively in your programs.

Sarthak Sehgal

Sarthak Sehgal is a tech lead at a high frequency options market maker. His interests lie in low level programming, performance optimization, and finance. Blog: sartech.substack.com