--- description: "Learn more about: _aligned_malloc" title: "_aligned_malloc" ms.date: "4/2/2020" api_name: ["_aligned_malloc", "_o__aligned_malloc"] api_location: ["msvcrt.dll", "msvcr80.dll", "msvcr90.dll", "msvcr100.dll", "msvcr100_clr0400.dll", "msvcr110.dll", "msvcr110_clr0400.dll", "msvcr120.dll", "msvcr120_clr0400.dll", "ucrtbase.dll", "api-ms-win-crt-heap-l1-1-0.dll"] api_type: ["DLLExport"] topic_type: ["apiref"] f1_keywords: ["_aligned_malloc", "alligned_malloc"] helpviewer_keywords: ["aligned_malloc function", "_aligned_malloc function"] ms.assetid: fb788d40-ee94-4039-aa4d-97d73dab1ca0 --- # `_aligned_malloc` Allocates memory on a specified alignment boundary. ## Syntax ```C void * _aligned_malloc( size_t size, size_t alignment ); ``` ### Parameters *`size`*\ Size of the requested memory allocation. *`alignment`*\ The alignment value, which must be an integer power of 2. ## Return value A pointer to the memory block that was allocated or `NULL` if the operation failed. The pointer is a multiple of *`alignment`*. ## Remarks **`_aligned_malloc`** is based on [`malloc`](malloc.md). **`_aligned_malloc`** is marked `__declspec(noalias)` and `__declspec(restrict)`, meaning that the function is guaranteed not to modify global variables and that the pointer returned isn't aliased. For more information, see [`noalias`](../../cpp/noalias.md) and [`restrict`](../../cpp/restrict.md). This function sets `errno` to `ENOMEM` if the memory allocation failed or if the requested size was greater than `_HEAP_MAXREQ`. For more information about `errno`, see [`errno`, `_doserrno`, `_sys_errlist`, and `_sys_nerr`](../errno-doserrno-sys-errlist-and-sys-nerr.md). Also, **`_aligned_malloc`** validates its parameters. If *`alignment`* isn't a power of 2 or *`size`* is zero, this function invokes the invalid parameter handler, as described in [Parameter validation](../parameter-validation.md). If execution is allowed to continue, this function returns `NULL` and sets `errno` to `EINVAL`. Use [`_aligned_free`](aligned-free.md) to deallocate memory obtained by both **`_aligned_malloc`** and `_aligned_offset_malloc`. Don't use `free`, which doesn't reclaim the aligned memory correctly and can lead to hard-to-diagnose bugs. By default, this function's global state is scoped to the application. To change this behavior, see [Global state in the CRT](../global-state.md). ## Requirements | Routine | Required C header | C++ header | |---|---|---| | **`_aligned_malloc`** | `` | `` | ## Example ```C // crt_aligned_malloc.c #include #include int main() { void *ptr; size_t alignment, off_set; // Note alignment should be 2^N where N is any positive int. alignment = 16; off_set = 5; // Using _aligned_malloc ptr = _aligned_malloc(100, alignment); if (ptr == NULL) { printf_s( "Error allocation aligned memory."); return -1; } if (((unsigned long long)ptr % alignment ) == 0) printf_s( "This pointer, %p, is aligned on %zu\n", ptr, alignment); else printf_s( "This pointer, %p, is not aligned on %zu\n", ptr, alignment); // Using _aligned_realloc ptr = _aligned_realloc(ptr, 200, alignment); if ( ((unsigned long long)ptr % alignment ) == 0) printf_s( "This pointer, %p, is aligned on %zu\n", ptr, alignment); else printf_s( "This pointer, %p, is not aligned on %zu\n", ptr, alignment); _aligned_free(ptr); // Using _aligned_offset_malloc ptr = _aligned_offset_malloc(200, alignment, off_set); if (ptr == NULL) { printf_s( "Error allocation aligned offset memory."); return -1; } if ( ( (((unsigned long long)ptr) + off_set) % alignment ) == 0) printf_s( "This pointer, %p, is offset by %zu on alignment of %zu\n", ptr, off_set, alignment); else printf_s( "This pointer, %p, does not satisfy offset %zu " "and alignment %zu\n",ptr, off_set, alignment); // Using _aligned_offset_realloc ptr = _aligned_offset_realloc(ptr, 200, alignment, off_set); if (ptr == NULL) { printf_s( "Error reallocation aligned offset memory."); return -1; } if ( ( (((unsigned long long)ptr) + off_set) % alignment ) == 0) printf_s( "This pointer, %p, is offset by %zu on alignment of %zu\n", ptr, off_set, alignment); else printf_s( "This pointer, %p, does not satisfy offset %zu and " "alignment %zu\n", ptr, off_set, alignment); // Note that _aligned_free works for both _aligned_malloc // and _aligned_offset_malloc. Using free is illegal. _aligned_free(ptr); } ``` ```Output This pointer, 3280880, is aligned on 16 This pointer, 3280880, is aligned on 16 This pointer, 3280891, is offset by 5 on alignment of 16 This pointer, 3280891, is offset by 5 on alignment of 16 ``` ## See also [Data alignment](../data-alignment.md)