nothrow new

The new operator is used to allocate memory at runtime. When the operator is unable to allocate that memory (maybe due to out of space), an exception is thrown (bad_alloc).

But what if we don’t use exceptions in our application? In that case, we can specify the nothrow constant when we attempt to allocate memory. This will call an overloaded function of the operator new, which returns a null pointer in case of error.

This works because the new operator is overloaded to accept a parameter of type nothrow_t (which is the type of std::nothrow). It looks strange as it is, but it actually calls operator new(size_t, std::nothrow_t).

Below you can find the function declaration, in VS2019 (MSVC)

You may also like...