繁体   English   中英

当 new 运算符重载并输出到 std::cout 时,用 Clang 编译的 C++ 程序崩溃

[英]C++ program compiled with Clang crashes when new operator is overloaded and outputs to std::cout

我在 Windows 10 上使用 Clang 版本 10.0.0。

这个程序

#include <iostream>
// without this operator the program works just fine
void* operator new(std::size_t nrOfBytes) {
    std::cout << "allocate " << nrOfBytes << " bytes on heap" << std::endl;
    void* p = malloc(nrOfBytes);
    if (p) {
        return p;
    } else {
        throw std::bad_alloc{};
    }
}
int main() {
    printf("START\n");
    return 0;
}

使用编译后返回代码 -1073741819 崩溃

clang++ Main.cpp -std=c++17

当然,当没有重载的 new 运算符时,同样调用 Clang 会生成一个无错误的程序。

任何提示?

尝试从“new”中删除 cout 操作。 可能是一些流操作需要其他“新”吗?

暂无
暂无

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

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