NAME
aligned_alloc —
allocate memory with a specified alignment
SYNOPSIS
#include
<stdlib.h>
void *aligned_alloc(size_t alignment, size_t size);
DESCRIPTION
[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(3), free(3),
malloc(3),
[ADV]
posix_memalign(3), [CX]
reallocarray(3), and realloc(3) that
allocate or deallocate a particular region of memory shall occur in a single
total order (see V1_chap04(7)), and each such deallocation
call shall synchronize with the next allocation (if any) in this order.
RETURN VALUE
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.
ERRORS
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.
EXAMPLES
None.
APPLICATION USAGE
None.
RATIONALE
See the RATIONALE for malloc(3).
FUTURE DIRECTIONS
None.
SEE ALSO
calloc(3), free(3), getrlimit(3), malloc(3), posix_memalign(3), realloc(3)
XBD <stdlib.h>
CHANGE HISTORY
First released in Issue 8. Included for alignment with the ISO/IEC 9899:2018 standard.