Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic aligned memory allocation in C++11

Tags:

c++

c++11

posix_memalign and _aligned_malloc on Windows allow to dynamically allocate an aligned chunk of memory. Is there anything similar in C++11? As far as I know, the alignas keyword only works with statically allocated objects.

like image 683
user882903 Avatar asked Aug 07 '11 16:08

user882903


People also ask

What is dynamic memory allocation in C programming?

C dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc and free.

What is dynamically allocating memory?

Allocation of memory at the time of execution (run time) is known as dynamic memory allocation. The functions calloc() and malloc() support allocating of dynamic memory. Dynamic allocation of memory space is done by using these functions when value is returned by functions and assigned to pointer variables.

Does C have dynamic memory allocation?

This is known as dynamic memory allocation in C programming. To allocate memory dynamically, library functions are malloc() , calloc() , realloc() and free() are used. These functions are defined in the <stdlib. h> header file.

Is malloc memory aligned?

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.


2 Answers

It depends on what alignment you require. For anything <= to alignof(std::max_align_t), new works as per n3242 3.7.4.1/2:

The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type with a fundamental alignment requirement

std::max_align_t is a complete object type with the strictest fundamental alignment.

Note that allocation of arrays of char or unsigned char but not signed char have a different rule in 5.3.4/10:

For arrays of char and unsigned char, the difference between the result of the new-expression and the address returned by the allocation function shall be an integral multiple of the strictest fundamental alignment requirement (3.11) of any object type whose size is no greater than the size of the array being created.

So new char[1]; can have an alignment of 1.

As for allocating memory with an alignment greater than alignof(std::max_align_t), C++11 provides no direct way to do this. The only reliable way is to allocate at least size + alignment bytes and use std::align to get a correctly aligned location in this buffer.

This can waste a lot of memory, so if you need a lot of these, you could create an allocator that allocates a chunk large enough for all of them and use std::align on that. Your overhead is then amortized across all of the allocations.

Your other option is to wait for http://open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3396.htm to make it into the standard.

Personally I would just write an abstraction layer over the OS provided APIs for allocating aligned memory.

like image 68
Michael Spencer Avatar answered Sep 23 '22 13:09

Michael Spencer


You can use posix_memalign/_aligned_malloc to allocate a piece of memory and then using the special 'new' operator syntax to initialize an object in that memory region. Something like this:

// Allocate raw memory for a Foo object. void *mem; size_t alignment = 0x1000; size_t size = ?; posix_memalign(&mem, alignment, size); // Call the constructor on the allocated memory. Foo *foo = new (mem) Foo(...);  // Now you have a useable object. foo->some_method();  // Call destructor without freeing object memory. foo->~Foo(); // Free raw memory. free(foo); 
like image 36
Hongli Avatar answered Sep 22 '22 13:09

Hongli