C++ Reference ยท Keywords and Statements
The alignas specifier and alignof operator
Reference for C++ alignas and alignof: what alignment means, padding in structs, over-aligned types, aligned allocation and std::max_align_t.
Every object type has an alignment requirement: its address must be a multiple of some power of two, usually the size of the type up to a limit. A 4-byte int typically needs an address divisible by 4. CPUs load aligned data fastest, and some instructions (SIMD loads, for example) require it. The compiler inserts padding inside structs to satisfy these rules, which is why sizeof of a struct is often larger than the sum of its members.
alignof(T) tells you the alignment of a type. alignas(N) lets you request a stricter alignment for a variable, a data member or a whole class, for instance to put each counter on its own cache line or to meet SIMD requirements.
alignas(32) float lane[8]; // address multiple of 32 struct alignas(64) Counter { int n; }; // every Counter starts on a 64-byte boundary constexpr std::size_t a = alignof(double);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
alignas(constant-expression) // numeric alignment, a power of two alignas(type-id) // same as alignas(alignof(type)) alignas(Ts...) // pack expansion: the strictest one wins alignof(type-id) // std::size_t, constant expression
| Name | Description |
|---|---|
alignas(N) |
Requires an alignment of at least N bytes. N must be zero (no effect) or a power of two supported by the implementation. |
alignas(T) |
Shorthand for alignas(alignof(T)). |
alignof(T) |
Alignment of T in bytes, as std::size_t. For a reference type it is the alignment of the referenced type; for an array, that of the element. |
std::alignment_of_v<T> |
The same value as a type trait. |
std::max_align_t |
A type whose alignment is the largest the default new is guaranteed to honor for ordinary types. |
Errors / undefined behavior: alignas may not weaken alignment below the natural one (that is ill-formed). An alignment that the implementation does not support is ill-formed or, for dynamic allocation, makes new fail. Accessing an object through a misaligned pointer is undefined behavior, even on CPUs that tolerate it.
Usage notes
- Order members from large to small to reduce padding. Compare the two structs in the first example: the same data takes 24 bytes in one order and 16 in the other (on a typical 64-bit ABI).
- Struct size is a multiple of its alignment, so arrays of the struct keep every element aligned.
- Over-aligned types (alignment greater than
__STDCPP_DEFAULT_NEW_ALIGNMENT__, usually 16) are handled correctly bynewsince C++17. Before that, you neededstd::aligned_allocor custom allocators. std::aligned_alloc(alignment, size)requires the size to be a multiple of the alignment and the memory is released withstd::free.- False sharing. Two threads writing to variables on the same cache line slow each other down. Aligning hot, independently-written objects to the cache line fixes it.
std::hardware_destructive_interference_sizeis the portable name for that size, but GCC warns when it is used in a header; many projects hard-code 64. alignason a variable affects only that object; on a type it affects all objects of that type, including members and array elements.alignof(T)is a constant expression, so it is usable instatic_assert. Seesizeofandalignof.alignasandalignofcome from C++11; C23 has the same names as keywords.