C Reference ยท stdlib.h
C aligned_alloc() function
Reference for C11 aligned_alloc(): allocate heap memory with a stricter alignment than malloc, the size-multiple rule, alignof and how to free the block.
aligned_alloc() allocates memory whose address is a multiple of an alignment you choose. Plain malloc() guarantees alignment only up to that of the strictest ordinary type (usually 16 bytes on 64-bit systems). You need more when code works on cache lines (64 bytes), SIMD vectors (32 or 64 bytes), page-sized buffers (4096 bytes) or hardware that requires aligned addresses.
It was added in C11 and is released with the normal free(). You can check what alignment a type needs with alignof (a keyword in C23, a macro from <stdalign.h> before that).
#include <stdlib.h> void *aligned_alloc(size_t alignment, size_t size);
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
void *aligned_alloc(size_t alignment, size_t size);
| Name | Description |
|---|---|
alignment |
The required alignment in bytes. It must be a power of two and a valid alignment supported by the implementation. |
size |
The block size in bytes. C11 required it to be a multiple of alignment; C17 and later (and glibc) are more lenient, but staying with a multiple is the portable choice. |
Returns: a pointer aligned to alignment, or NULL on failure (including an unsupported alignment).
Errors / undefined behavior: an invalid alignment makes the call fail (NULL) in modern libraries; in older C11 implementations it was undefined behavior. Freeing the block with anything but free() is undefined. Pointer values are shown here only as a yes/no test, since the address itself changes from run to run.
Usage notes
- Alignment must be a power of two. 8, 16, 32, 64, 4096 are fine; 24 is not.
- Round the size up to a multiple of the alignment. It costs a few bytes and keeps you within the C11 rules and on old libraries.
- Compatible with
free(). Unlike Windows'_aligned_malloc, which needs_aligned_free, the C11 function is paired with plainfree. - POSIX alternative:
posix_memalign(&p, alignment, size)returns an error code instead ofNULL, has no size-multiple rule, and needs the alignment to be a multiple ofsizeof(void *). - Stack and static data use
alignas(64) char buf[256];(C23 keyword;_Alignasbefore) instead of the heap. realloc()does not preserve a stricter alignment. Resizing may return a block with only the default alignment. Allocate a new aligned block and copy.- C23:
free_aligned_sized()can be paired with it on compilers whose library provides it; it is not available in the runner's glibc. - Do not over-align. Huge alignments waste memory because the allocator has to skip bytes.
Related
malloc,free,alignofandalignas- Lessons: Padding and alignment, Dynamic memory