The Open Group Base Specifications Issue 8
IEEE Std 1003.1-2024
Copyright © 2001-2024 The IEEE and The Open Group

NAME

aligned_alloc — allocate memory with a specified alignment

SYNOPSIS

#include <stdlib.h>

void *aligned_alloc(size_t
alignment, size_t size);

DESCRIPTION

[CX] [Option Start] 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. [Option End]

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] [Option Start]  posix_memalign(), [Option End]
[CX] [Option Start] reallocarray(), [Option End]  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.

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] [Option Start]  and set errno to indicate the error. [Option End]

ERRORS

The aligned_alloc() function shall fail if:

[EINVAL]
[CX] [Option Start] The value of alignment is not a valid alignment supported by the implementation. [Option End]
[ENOMEM]
[CX] [Option Start] Insufficient storage space is available. [Option End]

The aligned_alloc() function may fail if:

[EINVAL]
[CX] [Option Start] size is 0 and the implementation does not support 0 sized allocations. [Option End]

The following sections are informative.

EXAMPLES

None.

APPLICATION USAGE

None.

RATIONALE

See the RATIONALE for malloc .

FUTURE DIRECTIONS

None.

SEE ALSO

calloc , free , getrlimit , malloc , posix_memalign , realloc

XBD <stdlib.h>

CHANGE HISTORY

First released in Issue 8. Included for alignment with the ISO/IEC 9899:2018 standard.

End of informative text.

 

return to top of page

UNIX® is a registered Trademark of The Open Group.
POSIX™ is a Trademark of The IEEE.
Copyright © 2001-2024 The IEEE and The Open Group, All Rights Reserved
[ Main Index | XBD | XSH | XCU | XRAT ]