KeyC0de
KeyC0de

Reputation: 5277

How to call the overloaded aligned new and delete operators in `C++17`?

From cppreference we can see several new overloads of new and delete, as well as new[] and delete[] were added. I can't find any examples of usage with the new aligned overloads, neither on cppreference nor anywhere else. I've experimented with them for quite some time now and I can't find out how to trigger these aligned dynamically allocated calls. Anyone has any idea, kindly share an example.

Upvotes: 6

Views: 2989

Answers (3)

zkoza
zkoza

Reputation: 2869

I guess the question was how to call an overloaded new explicitly, whereas all answers so far advised how to do it implicitly.

My solution (floats aligned to 256-byte boundaries):

auto q = new (std::align_val_t(256)) float;
auto p = new (std::align_val_t(256)) float[10];

Explanation

We go to https://en.cppreference.com/w/cpp/language/new ("new expression') and navigate to section "Placement new":

If placement_params are provided, they are passed to the allocation function as additional arguments

That's it!

Well, almost. Here: https://en.cppreference.com/w/cpp/memory/new/operator_new we read:

These allocation functions are called by new-expressions to allocate memory in which new object would then be initialized. They may also be called using regular function call syntax.

I was intrigued by the possibility of calling operator new using function call syntax. I don't think anyone does it like this. Let's try:

auto r = operator new (sizeof(float), std::align_val_t(256));
auto s = operator new[] (sizeof(float)*10, std::align_val_t(256)); // don't do it!!!

Ugly and dangerous, especially in the array-like version, as it does not have a place for the argument corresponding to the number of requested elements -- all it needs is the number of bytes to allocate, which may require taking into account some alignment overhead.

Upvotes: 2

Antony Peacock
Antony Peacock

Reputation: 487

You need to specify the align as keyword on your type and then just call new and delete as normal. I have put together an article with examples about it here: https://github.com/Twon/Alignment/blob/master/docs/alignment_in_C%2B%2B.md. An example is:

#include <memory>

int main() {
    class alignas(16) float4 {
        float f[4];
    }; 

    std::unique_ptr<float4 > aligned_vec4(std::make_unique<float4 >());
}

And an example with the Intel compiler which currently make this feature available via the aligned_new extension header: https://software.intel.com/en-us/articles/aligned-operator-new-support-in-intel-c-compiler

Upvotes: 7

Buddy
Buddy

Reputation: 11038

It’s on cppreference, just buried a few links deep: https://en.cppreference.com/w/cpp/language/new

new(2,f) T; // calls operator new(sizeof(T), 2, f)
        // (C++17) or operator new(sizeof(T), std::align_val_t(alignof(T)), 2, f)

More information on it: https://www.bfilipek.com/2017/06/cpp17-details-clarifications.html Looks like you actually use the alignas keyword and it will automatically call the new new.

Upvotes: 2

Related Questions