关于operator new重载和异常的问题

Question about operator new overload and exception

为什么这个 code snippet 会输出很多“这里”?

我认为程序应该在 throw std::invalid_argument( "fool" ); 被调用后终止。

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

std::invalid_argumentdocumentation掌握了线索:

Because copying std::invalid_argument is not permitted to throw exceptions, this message is typically stored internally as a separately-allocated reference-counted string. This is also why there is no constructor taking std::string&&: it would have to copy the content anyway.

您可以看到字符串参数是有意复制的。这意味着如果您以这种方式抛出此异常,则几乎可以保证 re-entering new

你还应该知道 malloc 可以 return nullptr 这将违反 operator new 的设计,它应该 return 一个有效的指针或扔.

在这种情况下抛出的正常异常类型是 std::bad_alloc。我无法想象你为什么要扔 std::invalid_argument。我想也许您在某处的构造函数中遇到了这个问题,因此决定测试分配本身。

从技术上讲,您可以通过传递 default-constructed 字符串作为参数来解决问题:

// ... but, why would you do this?!! :(
throw std::invalid_argument(std::string());  // will not allocate

呃,恶心。我建议您找到一个更合适的异常来抛出(如果您确实需要一个),或者创建您自己的 non-allocating 异常。