std::aligned_alloc(3)
π std::aligned_alloc(3) π
Okay bestie, welcome to the C++ Standard Libary page for std::aligned_alloc β yes, the name is literally the same as the thing, and honestly? Iconic.
β¨ NAME β¨
std::aligned_alloc β std::aligned_alloc. That's the whole NAME section. Periodt.
πΈ Synopsis πΈ
Defined in header <cstdlib>:
void* aligned_alloc( std::size_t alignment, std::size_t size );
(since C++17)
This allocates size bytes of uninitialized storage whose alignment is specified by alignment (implicitly creating objects in the destination area). The size parameter must be an integral multiple of alignment.
Now the thread-safety tea β: these functions are required to be thread-safe:
- The library versions of
operator newandoperator delete - User replacement versions of global
operator newandoperator delete std::calloc,std::malloc,std::realloc,std::aligned_alloc,std::free
Calls to these functions that allocate or deallocate a particular unit of storage occur in a single total order, and each such deallocation call happens-before the next allocation (if any) in this order.
π Parameters π
alignmentβ specifies the alignment. Must be a valid alignment supported by the implementation.sizeβ number of bytes to allocate. An integral multiple ofalignment.
π¦ Return value π¦
On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated with std::free or std::realloc.
On failure, returns a null pointer.
π Notes π
Passing a size which is not an integral multiple of alignment, or an alignment which is not valid or not supported by the implementation, causes the function to fail and return a null pointer. (C11, as published, specified undefined behavior in this case, this was corrected by DR460.)
As an example of the "supported by the implementation" requirement, POSIX function posix_memalign accepts any alignment that is a power of two and a multiple of sizeof(void*), and POSIX-based implementations of aligned_alloc inherit this requirements.
Fundamental alignments are always supported. If alignment is a power of two and not greater than alignof(std::max_align_t), aligned_alloc may simply call std::malloc.
Regular std::malloc aligns memory suitable for any object type with a fundamental alignment. This function is useful for over-aligned allocations, such as to SSE, cache line, or VM page boundary.
This function is not supported in Microsoft C Runtime library because its implementation ofstd::freeis unable to handle aligned allocations of any kind. Instead, MS CRT provides_aligned_malloc(to be freed with_aligned_free).
π Example π
// Run this code
#include <cstdio>
#include <cstdlib>
int main()
{
int* p1 = static_cast<int*>(std::malloc(10 * sizeof *p1));
std::printf("default-aligned address: %p\n",
static_cast<void*>(p1));
std::free(p1);
int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
std::printf("1024-byte aligned address: %p\n",
static_cast<void*>(p2));
std::free(p2);
}
Possible output:
default-aligned address: 0x2221c20
1024-byte aligned address: 0x2222400
π See also π
aligned_storageβ defines the type suitable for use as uninitialized storage for types of given size. It's a class template,(C++11)(deprecated in C++23).- C documentation for
aligned_allocβ yes, there's a C version too, bestie.
Okay that's the whole page! Source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::aligned_alloc(3) π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms