简体   繁体   English

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

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

I am using Clang version 10.0.0 on Windows 10.我在 Windows 10 上使用 Clang 版本 10.0.0。

This program这个程序

#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;
}

crashes with return code -1073741819 after having been compiled with使用编译后返回代码 -1073741819 崩溃

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

Of course, the very same invocation of Clang produces an error-free program when there is no overloaded new operator.当然,当没有重载的 new 运算符时,同样调用 Clang 会生成一个无错误的程序。

Any hints ?任何提示?

Try to delete cout operations from "new".尝试从“new”中删除 cout 操作。 May be some stream operations need other "new"?可能是一些流操作需要其他“新”吗?

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

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