C Reference ยท Keywords and Statements
The alignof and alignas keywords
Reference for C alignof and alignas: query the alignment of a type, request stricter alignment for objects, and how alignment affects struct layout.
Every object type in C has an alignment requirement: its address must be a multiple of some power of two. alignof reports that number for a type, and alignas asks the compiler to give a variable or member a stricter (larger) alignment than it would normally have.
Alignment is why structs contain padding and why malloc returns memory aligned for any standard type. Both keywords are real keywords since C23; in C11 and C17 they are spelled _Alignof and _Alignas, with the macros alignof and alignas available from <stdalign.h>.
size_t a = alignof(double); alignas(16) unsigned char buffer[64];
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 scanf or fgets), 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 scanf or fgets), it gets the next line from this box.
Syntax
alignof(type-name) alignas(constant-expression) alignas(type-name)
| Part | Description |
|---|---|
alignof(type) |
Alignment requirement of the type, as a size_t constant. |
alignas(N) |
Align the declared object or member to N bytes. N is a power of two, or 0 to have no effect. |
alignas(type) |
Same as alignas(alignof(type)). |
Returns: alignof yields a size_t; alignas is a declaration specifier with no value.
Errors: alignas cannot be used on a bit-field, a function, a typedef, a parameter or a register object, and it must not request a weaker alignment than the type's natural alignment. Over-aligned requests beyond what the implementation supports are errors or implementation-defined.
Usage notes
- Natural alignment is usually the size of the type for scalars (
int4,double8 on common 64-bit platforms), and the largest member alignment for structs. - Struct size is a multiple of its alignment, so arrays of structs keep every element aligned. That is where trailing padding comes from.
- Typical uses: SIMD buffers (16, 32, 64 bytes), cache-line alignment to avoid false sharing, and buffers that hold arbitrary objects.
- Heap memory.
mallocreturns memory aligned for any fundamental type; for larger alignments usealigned_alloc. - Misaligned access is undefined behavior in C and a crash or slowdown on some CPUs, even if x86 often tolerates it.
- Keywords in C23, macros earlier. In C11/C17 include
<stdalign.h>foralignasandalignof; in C23 the header is still allowed and defines nothing new. max_align_t(from<stddef.h>) has the strictest fundamental alignment.
Related
sizeof, padding,aligned_alloc,<stdalign.h>- Lessons: Padding and alignment, Memory layout