gpt4 book ai didi

c++ - Placement new 正在写入比数组大小更多的字节

转载 作者:太空狗 更新时间:2023-10-29 20:52:11 27 4
gpt4 key购买 nike

在下面的程序中,我预计 10 个字节将被新放置覆盖,然后为每个字节调用析构函数:

#include <memory>

struct MyChar {
MyChar(char c = 'n') :c{c}{}
~MyChar(){ c = 'd'; }
char c;
};

int main()
{
{
MyChar first[10]{0,1,2,3,4,5,6,7,8,9};
new (first)MyChar[10]{10,11,12,13,14,15,16,17,18,19};
}
return 0;
}

但是编译器(*)警告说 18 会被写成:

warning C6386: Buffer overrun while writing to 'first': the writable size is '10' bytes, but '18' bytes might be written.

首先字节按预期写入: enter image description here

但编译器并不是在虚张声势。在 placement new 语句中它确实写入了 18 个字节: enter image description here

导致错误:

Run-Time Check Failure #2 - Stack around the variable 'first' was corrupted.

为什么不坚持 10 个字节? sizeof(MyChar)==1alignof(MyChar)==1


(*) Microsoft Visual Studio Community 2017 预览版 (2)。此外(但在编译期间没有警告)我在 Microsoft Visual Studio Community 2017(版本 15.2 (26430.15) 发布)上遇到相同的内存覆盖和运行时错误。

最佳答案

数组的新位置可能需要比 N * sizeof(Object) 更多的空间

(???因为编译器必须能够使用 delete[] 正确调用析构函数???)。

5.3.4 [expr.new]:

new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+y,2,f).

Here, x and y are non-negative unspecified values representing array allocation overhead; the result of the new-expression will be offset by this amount from the value returned by operator new[]. This overhead may be applied in all array new-expressions, including those referencing the library function operator new[](std::size_t, void*) and other placement allocation functions. The amount of overhead may vary from one invocation of new to another. —end example ]

关于c++ - Placement new 正在写入比数组大小更多的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46696675/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com