On this page
_Alignof
Prev: sizeof | Up: builtins | Next: _Generic
Rel: Arena Allocator Pattern, Memory Layout & ABI
Ref: cppreference — _Alignof
Tags: #public #prog #c #builtins #tier/standard
Availability
Built-in (Operator (C11; alignof in C23)) — requires no #include header.
Syntax & Overview
_Alignof(type)
Returns the alignment requirement in bytes of a type as size_t.
Notes & Pitfalls
- Returns alignment in bytes (
size_t): Essential for custom arena/pool allocators to avoid misaligned pointer UB. - C23 adds
alignofas a keyword (or#include <stdalign.h>in C11).
Example
#include <assert.h>
#include <stddef.h>
int main(void) {
size_t a = _Alignof(double);
assert(a >= 1 && (a & (a - 1)) == 0); // always a power of two
}
Linked from
Related notes
- tuts/c/builtins/_Generic Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/sizeof Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins Linked here; links to this page; shared tags: #builtins, #c, #prog
- tuts/c/compilation_and_internals/memory_layout_and_abi Linked here; shared tags: #c, #prog
- tuts/c/tutorials/arena_allocator_pattern Linked here; shared tags: #c, #prog
- tuts/c/builtins/__FILE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__func__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__LINE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__STDC_VERSION__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Atomic shared tags: #builtins, #c, #prog, #tier