Memory Alignment
Subject: Computer and Technology noun /ˈmɛm.ə.ri əˈlaɪn.mənt/ 1 definition
- computer-architecture
- memory-management
- systems-programming
- compiler-design
-
Definition (noun)
Entry #349 · UpdatedMemory Alignment is the placement of data in memory at starting addresses that are integer multiples of a fixed byte boundary, typically a power of two such as 4, 8, 16, or 64 bytes. Because modern processors fetch and store data across memory buses and CPU caches in fixed-width chunks—such as machine words, SIMD vector registers, or cache lines—proper alignment prevents a single value from straddling multiple hardware access units. Compilers and memory allocators enforce alignment automatically by choosing aligned base addresses and inserting unused padding bytes between fields in composite structures. Respecting these boundaries separates fast, atomic hardware access from unaligned layouts, which either force extra split-access cycles and break atomicity or raise hardware alignment faults on strict architectures and vector instructions.
Common Types
Natural Alignment: Placing a scalar or primitive value of size
Nbytes at an address divisible byN(such as a 4-byte integer at a 4-byte boundary or an 8-byte pointer at an 8-byte boundary), which compilers enforce by default using ABI rules and structure padding.SIMD and Vector Alignment: Constraining array and tensor buffers to 16-, 32-, or 64-byte boundaries so Single Instruction, Multiple Data (SIMD) instructions such as x86 AVX2 or ARM NEON loads can fill vector registers in one operation without alignment faults.
Cache-Line Alignment: Aligning independent, concurrently updated variables to the processor's cache-line size (typically 64 bytes) using directives such as C++
alignas(64)to prevent false sharing across CPU cores.Page Alignment: Allocating large buffers on operating-system virtual memory page boundaries (commonly 4 KiB or 2 MiB) via APIs like
posix_memalign()ormmap()to support hardware DMA transfers,O_DIRECTstorage I/O, and page-level memory protection.
Example
In a C++ trading engine on x86-64, defining
struct Order { uint8_t side; uint64_t order_id; }causes the compiler to insert 7 padding bytes aftersideat offset0soorder_idlands on the 8-byte-aligned offset8, increasingsizeof(Order)from9to16bytes. To prevent false sharing across CPU cores and let AVX2 intrinsics such as_mm256_load_si256load price batches without alignment faults, the engine marks shared counters withalignas(64)and allocates vector buffers withstd::aligned_alloc(32, 4096).