Search code examples
c++clangnew-operatorcoutclang++

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 ?


Solution

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