简体   繁体   English

如何在`C++17`中调用重载对齐的new和delete运算符?

[英]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.cppreference我们可以看到newdelete几个新重载,以及new[]delete[]被添加。 I can't find any examples of usage with the new aligned overloads, neither on cppreference nor anywhere else.我在 cppreference 或其他任何地方都找不到任何使用新对齐重载的示例。 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.任何人都有任何想法,请分享一个例子。

You need to specify the align as keyword on your type and then just call new and delete as normal.您需要在类型上指定 align as 关键字,然后正常调用 new 和 delete 。 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 .我在这里整理了一篇带有示例的文章: 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以及英特尔编译器的示例,该编译器当前通过aligned_new 扩展标头提供此功能: https ://software.intel.com/en-us/articles/aligned-operator-new-support-in-intel-c-compiler

I guess the question was how to call an overloaded new explicitly, whereas all answers so far advised how to do it implicitly.我想问题是如何显式调用重载的new ,而到目前为止所有的答案都建议如何隐式调用。

My solution (floats aligned to 256-byte boundaries):我的解决方案(浮动对齐到 256 字节边界):

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":我们转到https://en.cppreference.com/w/cpp/language/new (“新表达式”)并导航到“Placement new”部分:

If placement_params are provided, they are passed to the allocation function as additional arguments如果提供了placement_params,它们将作为附加参数传递给分配函数

That's it!就是这样!

Well, almost.嗯,差不多。 Here: https://en.cppreference.com/w/cpp/memory/new/operator_new we read:这里: https : //en.cppreference.com/w/cpp/memory/new/operator_new我们读到:

These allocation functions are called by new-expressions to allocate memory in which new object would then be initialized.这些分配函数由 new 表达式调用以分配内存,然后将在其中初始化新对象。 They may also be called using regular function call syntax.也可以使用常规函数调用语法来调用它们。

I was intrigued by the possibility of calling operator new using function call syntax.我对使用函数调用语法调用operator new的可能性很感兴趣。 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.丑陋而危险,尤其是在类似数组的版本中,因为它没有与请求元素数量相对应的参数的位置——它所需要的只是要分配的字节数,这可能需要考虑一些对齐高架。

It's on cppreference, just buried a few links deep: https://en.cppreference.com/w/cpp/language/new它在 cppreference 上,只是深埋了几个链接: 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 .关于它的更多信息: https : alignas看起来您实际上使用了alignas关键字,它会自动调用新的new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM