Data Structure Alignment and Memory Padding
Modern CPUs don’t read memory one byte at a time. They fetch data in aligned chunks, typically 4 or 8 bytes at once, and this hardware constraint forces compilers to insert invisible padding bytes into your data structures. The result is that a struct you think occupies 9 bytes might actually consume 12 or 16 bytes in memory.
Why CPUs Care About Alignment
When a CPU reads from memory, it accesses data along address boundaries that match the bus width. A 64-bit processor prefers to read 8-byte chunks starting at addresses divisible by 8. If you store a 4-byte integer at address 0x1001, the CPU might need to perform two separate memory reads and combine them with bit-shifting to extract your value. This unaligned access is either slow or, on some architectures like older ARM chips, triggers a hardware exception.
The performance penalty is real. An unaligned read can take 2-3x longer than an aligned one, and in tight loops processing millions of structs, that overhead compounds. Compilers know this and automatically insert padding to ensure each field lands on a natural boundary.
How Padding Gets Inserted
Consider a simple struct with a char (1 byte), an int (4 bytes), and a short (2 bytes). You’d expect 7 bytes total. But the compiler will typically arrange it like this:
struct Example {
char a; // 1 byte
// 3 bytes padding
int b; // 4 bytes
short c; // 2 bytes
// 2 bytes padding (for array alignment)
};
// Total: 12 bytes
The int needs 4-byte alignment, so 3 padding bytes follow the char. The final 2 bytes ensure that if you create an array of these structs, each element’s fields remain properly aligned. The struct’s total size gets rounded up to a multiple of its strictest alignment requirement.
Field order matters significantly. Rearranging the example to {int, short, char} would still need padding but might pack more efficiently on some platforms. Many C codebases explicitly reorder struct fields from largest to smallest to minimize waste.
Platform Differences
Alignment rules vary by architecture and ABI. x86-64 is relatively forgiving and handles some misalignment in hardware, though with a performance cost. ARM and RISC processors tend to be stricter. A double typically requires 8-byte alignment on 64-bit systems but might only need 4-byte alignment on 32-bit ARM with certain compiler flags.
Operating systems also define packing rules through their ABIs. Windows, Linux, and macOS can differ in how they align the same struct definition, which is why careful serialization formats like Protocol Buffers explicitly define field layouts instead of relying on C struct memory dumps.
Practical Implications
The invisible padding has several downstream effects. Cache lines are typically 64 bytes, and poor struct layout can cause multiple structs to share a cache line, leading to false sharing in multithreaded code. Two threads modifying separate struct instances on the same cache line will bounce that line between CPU cores, destroying performance.
Memory usage balloons with poor field ordering. A struct with many small fields interleaved with larger ones can waste 30-40% of its space on padding. In systems allocating millions of instances, that’s real RAM and cache pressure.
Some languages and compilers offer packed struct attributes that suppress padding. This makes structs smaller but forces the compiler to emit slower unaligned access code. It’s a deliberate tradeoff, typically used for binary file formats or network protocols where layout control matters more than CPU efficiency.
Inspecting and Optimizing Layouts
Modern compilers provide warnings and attributes for analyzing padding. GCC and Clang have flags that report struct layouts with padding visualized. Rust’s standard library documentation shows the size and alignment of types explicitly. Tools like pahole parse debugging information to show exactly where padding sits.
For performance-critical code, measuring is essential. Profile your cache miss rates and memory bandwidth before and after reordering fields. Sometimes the “obvious” optimization makes things worse by disrupting access patterns the CPU was prefetching successfully.
The key insight is that memory layout isn’t free. The physical reality of how CPUs and buses work forces a gap between the abstract data model in your code and the actual bytes in RAM. Understanding alignment lets you write more efficient data structures and debug surprising size discrepancies when they appear.