Skip to main content
Definition Website Computer and Technology

Memory Alignment

Subject: Computer and Technology noun /ˈmɛm.ə.ri əˈlaɪn.mənt/ 1 definition

  1. Definition (noun)

    Entry #349 · Updated

    Memory 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 N bytes at an address divisible by N (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() or mmap() to support hardware DMA transfers, O_DIRECT storage 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 after side at offset 0 so order_id lands on the 8-byte-aligned offset 8, increasing sizeof(Order) from 9 to 16 bytes. To prevent false sharing across CPU cores and let AVX2 intrinsics such as _mm256_load_si256 load price batches without alignment faults, the engine marks shared counters with alignas(64) and allocates vector buffers with std::aligned_alloc(32, 4096).