aligned_alloc3
Allocate aligned memory
Usage
#include <stdlib.h>
void *aligned_alloc(size_t alignment, size_t size);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
aligned_alloc():
_ISOC11_SOURCE
Library
Standard C library (libc, -lc)
Overview
aligned_alloc() allocates size bytes and returns a pointer to the allocated memory. The memory address will be a multiple of alignment, which must be a power of two. This address can later be successfully passed to free(3).
The memory is not zeroed.
Return value
aligned_alloc() returns a pointer to the allocated memory on success. On error, NULL is returned, and errno is set to indicate the error.
Errors
EINVAL
|
The alignment argument was not a power of two. |
|---|---|
ENOMEM
|
Out of memory. |
Attributes
For an explanation of the terms used in this section, see attributes(7).
| Interface | Attribute | Value |
| aligned_alloc() | Thread safety | MT-Safe |
Notes
On many systems there are alignment restrictions, for example, on buffers used for direct block device I/O. POSIX specifies the pathconf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed. Now one can use aligned_alloc() to satisfy this requirement.
The glibc malloc(3) always returns 8-byte aligned memory addresses, so this function is needed only if you require larger alignment values.
Releases
8 releases since Debian 7 (Wheezy); the text changed 7 times.