简体   繁体   中英

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.

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

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.

Any hints ?

Try to delete cout operations from "new". May be some stream operations need other "new"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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