---
name: "aligned_alloc"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/aligned_alloc"
---

# aligned\_alloc(3)

allocate aligned memory

## NAME

aligned\_alloc - allocate aligned memory

## LIBRARY

Standard C library (*libc*, *-lc*)

## SYNOPSIS

```
#include <stdlib.h>
```

**void \*aligned\_alloc(size\_t** *alignment***, size\_t** *size***);**

Feature Test Macro Requirements for glibc (see [**feature\_test\_macros**(7)](/feature_test_macros/7.md)):

**aligned\_alloc**():

```
    _ISOC11_SOURCE
```

## DESCRIPTION

**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)](/free/3.md).

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)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **aligned\_alloc**() | Thread safety | MT-Safe |

## STANDARDS

C23, POSIX.1-2024.

## HISTORY

glibc 2.16. C11, POSIX.1-2024.

### C11

In C11, the specification of this function had several issues ⟨https:\\://port70.net/\\:\\\[ti\]nsz/\\:c/\\:c11/\\:n1570.html#7.22.3.1p2⟩.

- •

  *size* had to be a multiple of *alignment*. Otherwise, the behavior was undefined.

- •

  If *alignment* was not a power of two, the behavior was undefined.

DR460 ⟨https:\\://www.open\\-std.org/\\:jtc1/\\:sc22/\\:wg14/\\:www/\\:docs/\\:summary.htm#dr\_460⟩ reported both cases of UB as unnecessarily dangerous, and fixed them with a Technical Corrigendum that transformed them into errors.

N2072 ⟨https:\\://www.open\\-std.org/\\:jtc1/\\:sc22/\\:wg14/\\:www/\\:docs/\\:n2072.htm⟩ reported that the requirement that *size* is a multiple of *alignment* is superfluous, and removed it with a Technical Corrigendum.

C17 incorporates both technical corrigenda. The API has been stable since C17.

glibc initially implemented it as silently aligning as *stdc\_bit\_ceil(alignment)* instead of *alignment*. Since glibc 2.38, it implements the C17 specification.

Some implementations, such as FreeBSD/jemalloc, implement the C17 specification, even though their documentation claims having undefined behavior.

Some implementations, such as OpenBSD, implement C11 amended with DR460, even though their documentation claims having undefined behavior.

No known implementations have exploited the undefined behavior in a more dangerous way. This function should be safe to use.

## 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)](/malloc/3.md) always returns 8-byte aligned memory addresses, so this function is needed only if you require larger alignment values.

## SEE ALSO

[**brk**(2)](/brk/2.md), [**getpagesize**(2)](/getpagesize/2.md), [**free**(3)](/free/3.md), [**malloc**(3)](/malloc/3.md)
