Developer Guide and Reference


Automatically-Aligned Dynamic Allocation


It is possible to tell the compiler that a data structure has a greater alignment requirement than its individual elements require. For example:
C++ standard syntax
class alignas(64) X { double elem[8]; };
GNU-compatible syntax
class __attribute__((aligned(64))) X { double elem[8]; };
Microsoft-compatible syntax
class __declspec(align(64)) X { double elem[8]; };
This is especially important for a structure that will be used with SIMD instructions, which typically require greater alignment than the individual data elements. The compiler will ensure that variables declared with such a type, either statically or on the stack, will be allocated with the appropriate alignment.
However, if an object of such a type is allocated dynamically, with a
-expression, the compiler was not previously able to do anything to ensure the appropriate alignment. That is because the C++ language requires that only very specific allocation methods be used, over which the programmer can take control if necessary, and none of those allocation methods are able to support specific alignment. They all assume that some alignment value is enough for everything, and guarantee that (and nothing more).
In the past, to ensure a greater alignment for a given type, a programmer had to take control of its allocation. One way to do that is by always allocating the memory separately with the appropriate alignment, and using a non-allocating placement
-expression. For example:
Incorrect alignment
new X
Correct alignment
new (_mm_malloc(sizeof(X), alignof(X))) X
However, this method is verbose, tedious, and error-prone.
Another way is to write class-specific allocation and deallocation functions—
operator new
operator delete
. For example:
class alignas(64) X { double elem[8]; public: void *operator new(size_t size){ return _mm_malloc(size, alignof(X)); } void operator delete(void *p){ return _mm_free(p); } };
This method is easier, because the changes are centralized in the class, instead of being distributed over the uses of the class. But to get it right in general is still fairly involved, because it requires defining several more functions, in case arrays of the class are dynamically allocated or
allocation is used.

Automatically-Aligned Dynamic Allocation

In this release of the compiler, all that is necessary in order to get correct dynamic allocation for aligned data is to include a new header:
#include <aligned_new>
After this header is included, a
-expression for any aligned type will automatically allocate memory with the alignment of that type.
On Windows*, it is possible to direct the compiler to include a file at the beginning of the primary source file, without modifying the source, using the
command-line option.

Implementation Details

This section explains the language rules for the new feature. If a program needs to take control of dynamic allocation and deallocation of aligned data for some reason other than alignment, this section explains how it can be done.
defines several new alignment-aware allocation and deallocation functions, each of which takes an alignment argument: