aligned_alloc — allocate memory with a specified alignment
#include <stdlib.h>
void *aligned_alloc(size_t alignment, size_t size);
[CX] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2024 defers to the ISO C standard.The aligned_alloc() function shall allocate unused space for an object whose alignment is specified by alignment, whose size in bytes is specified by size, and whose value is indeterminate.
The order and contiguity of storage allocated by successive calls to aligned_alloc() is unspecified. Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned shall point to the start (lowest byte address) of the allocated space. If the value of alignment is not a valid alignment supported by the implementation, a null pointer shall be returned. If the space cannot be allocated, a null pointer shall be returned. If the size of the space requested is 0, the behavior is implementation-defined: either a null pointer shall be returned to indicate an error, or the behavior shall be as if the size were some non-zero value, except that the behavior is undefined if the returned pointer is used to access an object.
For purposes of determining the existence of a data race, aligned_alloc() shall behave as though it accessed only memory locations accessible through its arguments and not other static duration storage. The function may, however, visibly modify the storage that it allocates. Calls to aligned_alloc(), calloc(), free(), malloc(), [ADV] posix_memalign(),
[CX] reallocarray(), and realloc() that allocate or deallocate a particular region of memory shall occur in a single total order (see 4.15.1 Memory Ordering ), and each such deallocation call shall synchronize with the next allocation (if any) in this order.
Upon successful completion, aligned_alloc() shall return a pointer to the allocated space; if size is 0, the application shall ensure that the pointer is not used to access an object.
Otherwise, it shall return a null pointer [CX] and set errno to indicate the error.
The aligned_alloc() function shall fail if:
- [EINVAL]
- [CX] The value of alignment is not a valid alignment supported by the implementation.
- [ENOMEM]
- [CX] Insufficient storage space is available.
The aligned_alloc() function may fail if:
- [EINVAL]
- [CX] size is 0 and the implementation does not support 0 sized allocations.
None.
None.
See the RATIONALE for malloc .
None.
calloc , free , getrlimit , malloc , posix_memalign , realloc
XBD <stdlib.h>
First released in Issue 8. Included for alignment with the ISO/IEC 9899:2018 standard.
return to top of page