mardi 8 mars 2022

Question about operator new overload and exception

Why this code snippet ouputs a lot of "here"?

I think the program should terminate when after throw std::invalid_argument( "fool" ); has been called.

#include <memory>
#include <iostream>

void* operator new(std::size_t size)
{
    std::cout << "here" << std::endl;
    throw std::invalid_argument( "fool" );   //commit out this, there would be many many ouputs
    return std::malloc(size);
}

void operator delete(void* ptr)
{
    return free(ptr);
}


int main()
{
    //std::unique_ptr<int> point2int(new int(999));

    int* rawpoint2int = new int(666);
}

Aucun commentaire:

Enregistrer un commentaire