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

标签 c++

在下面的程序中,我预计 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/

相关文章:

c++ - 使用带有事件循环和 QSocketNotifiers 的线程发出串行端口监视

c++ getline 似乎无法识别通过管道传输到标准输入的输入中的换行符

c++ - regex_replace 返回空字符串

c++ - 在默认构造函数中初始化 vector 变量?

c++ - 在 C++、Qt5 中创建自定义插槽

c++ - 在文本冒险中实现实时?

c++ - "(void) new"在 C++ 中是什么意思?

c++ - C++ 中的 register 关键字有什么问题?

c++ - 将对象转换为 dlsym 返回的地址

c++ - FileObject->FileName 不返回文件的完整路径