C aligned_alloc() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C aligned_alloc() function allocates dynamically aligned storage. It requests size bytes whose address satisfies the requested alignment. The storage is uninitialized, and the returned pointer has the same lifetime rules as malloc() storage.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. Success returns a suitably aligned pointer to uninitialized storage. Failure returns NULL; never dereference it, and free only a successful result. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete aligned_alloc() call:

C
aligned_alloc(64, 256);
CallWhat it does
aligned_alloc(64, 256);Allocates dynamically aligned storage using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
void *aligned_alloc(size_t alignment, size_t size);

Parameters

ParameterDescription
alignmentRequired alignment in bytes: a valid alignment supported by the implementation.
sizeNumber of bytes to allocate; in C11, it must be an integral multiple of alignment.

Return Value

Return ValueDescription
Non-null pointerAllocation succeeded and is aligned as requested.
NULLThe allocation could not be completed or the request is invalid.

C Examples

Example 1 — Allocate an integer array

This complete program demonstrates a distinct part of the aligned_alloc() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = aligned_alloc(64, 4 * 64);
    if (!p) return EXIT_FAILURE;
    for (int i=0;i<4;i++) p[i]=0;
    p[2]=15;
    printf("%d\n", p[2]);
    free(p); return 0;
}

How It Works

  1. Step 1. Calculate or state the requested storage size.
  2. Step 2. Call aligned_alloc() and test for NULL.
  3. Step 3. Initialize the element before reading it.
  4. Step 4. Release the allocation after its final use.

Example 2 — Fill every element

This complete program demonstrates a distinct part of the aligned_alloc() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = aligned_alloc(64, 4 * 64);
    if (!p) return EXIT_FAILURE;
    for (int i=0;i<4;i++) p[i]=(i+1)*10;
    printf("%d %d\n", p[0], p[3]);
    free(p); return 0;
}

How It Works

  1. Step 1. Obtain storage for four values.
  2. Step 2. Stop immediately if allocation failed.
  3. Step 3. Initialize each element within bounds.
  4. Step 4. Print two values, then free the block.

Example 3 — Handle allocation failure

This complete program demonstrates a distinct part of the aligned_alloc() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    void *p = aligned_alloc(64, ((size_t)-1 / 64) * 64);
    if (!p) {
        puts("allocation unavailable");
        return EXIT_FAILURE;
    }
    free(p); return EXIT_SUCCESS;
}

How It Works

  1. Step 1. Construct an impractically large request.
  2. Step 2. Call aligned_alloc() without assuming success.
  3. Step 3. Branch on NULL before any dereference.
  4. Step 4. Return a failure status when storage is unavailable.

Frequently Asked Questions

aligned_alloc() allocates dynamically aligned storage. It requests size bytes whose address satisfies the requested alignment. The storage is uninitialized, and the returned pointer has the same lifetime rules as malloc() storage.
Include <stdlib.h> before calling aligned_alloc().
Success returns a suitably aligned pointer to uninitialized storage. Failure returns NULL; never dereference it, and free only a successful result.
The C11 size argument must be a multiple of alignment. Release a successful allocation with free(), exactly once.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: at_quick_exit()

Continue through the C stdlib.h tutorials.

at_quick_exit() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful