Namespaces
Variants
Views
Actions

std::aligned_alloc

From cppreference.com
< cpp‎ | memory‎ | c
Revision as of 11:50, 24 June 2016 by Cubbi (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
Defined in header <cstdlib>
void* aligned_alloc( std::size_t alignment, std::size_t size );
(since C++17)

Allocate size bytes of uninitialized storage whose alignment is specified by alignment. The size parameter must be an integral multiple of alignment.

aligned_alloc is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage.

A previous call to std::free or std::realloc that deallocates a region of memory synchronizes-with a call to std::aligned_alloc that allocates the same or a part of the same region of memory. This synchronization occurs after any access to the memory by the deallocating function and before any access to the memory by std::aligned_alloc. There is a single total order of all allocation and deallocation functions operating on each particular region of memory.

Contents

Parameters

alignment - specifies the alignment. Must be a valid alignment supported by the implementation.
size - number of bytes to allocate. An integral multiple of alignment

Return value

On success, returns the pointer to the beginning of newly allocated memory. The returned pointer must be deallocated with free() or realloc().

On failure, returns a null pointer.

Notes

Passing a size which is not an integral multiple of alignment or a 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 DR 460).

As an example of the "supported by the implementation" requriement, 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.

Regular std::malloc aligns memory suitable for any object type (which, in practice, means that it is aligned to alignof(std::max_align_t)). This function is useful for over-aligned allocations, such as to SSE, cache line, or VM page boundary.

Example

#include <cstdio>
#include <cstdlib>
 
int main()
{
    int* p1 = std::malloc(10*sizeof *p1);
    std::printf("default-aligned addr:   %p\n", (void*)p1);
    free(p1);
 
    int* p2 = std::aligned_alloc(1024, 10*sizeof *p2);
    std::printf("1024-byte aligned addr: %p\n", (void*)p2);
    std::free(p2);
}

Possible output:

default-aligned addr:   0x17d6010
1024-byte aligned addr: 0x17d6400

See also

(C++11)(deprecated in C++23)
defines the type suitable for use as uninitialized storage for types of given size
(class template) [edit]