Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::allocator handle over-aligned types in C++17?

C++17 introduces std::aligned_alloc and alignment-aware new that can do over-aligned allocations, but what about std::allocator? Does it handle over-aligned types?

like image 789
Jamboree Avatar asked Sep 25 '17 07:09

Jamboree


People also ask

How aligned_ alloc works?

The aligned_alloc function allocates space for an object whose alignment is specified by alignment, whose size is specified by size, and whose value is indeterminate.

Does malloc return aligned memory?

Since malloc (or another dynamic memory allocator) is not necessarily guaranteed to align memory as we require, we'll need to perform two extra steps: Request extra bytes so we can returned an aligned address. Request extra bytes and store the offset between our original pointer and our aligned pointer.

What is memory alignment C++?

Alignment refers to the arrangement of data in memory, and specifically deals with the issue of accessing data as proper units of information from main memory. First we must conceptualize main memory as a contiguous block of consecutive memory locations. Each location contains a fixed number of bits.


1 Answers

In N4659(C++17 DIS), 23.10.9.1 [allocator.members], bullet 2

T* allocate(size_t n);

Returns: A pointer to the initial element of an array of storage of size n * sizeof(T), aligned appropriately for objects of type T.

Compared to C++14, the sentence

It is implementation-defined whether over-aligned types are supported

has been removed. So std::allocator should support over-aligned types in C++17.

like image 105
Jamboree Avatar answered Oct 30 '22 20:10

Jamboree