在下面的程序中,我预计 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.
但编译器并不是在虚张声势。在 placement new 语句中它确实写入了 18 个字节:
导致错误:
Run-Time Check Failure #2 - Stack around the variable 'first' was corrupted.
为什么不坚持 10 个字节? sizeof(MyChar)==1
和 alignof(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 ofoperator new[](sizeof(T)*5+y,2,f)
.Here,
x
andy
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 byoperator new[]
. This overhead may be applied in all array new-expressions, including those referencing the library functionoperator 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/