It's a questionable design, but you can certainly do this with a 'new-handler'. The default new-handler simply throws std::bad_alloc
. If the new-handler returns, new
will loop, and attempt to allocate again. It is also used by the nothrow new operator, but a std::bad_alloc
thrown by the new-handler is caught, and NULL
returned, in that case.
You simply need to set the new-handler to your custom void (*)()
handler function. At the very least, you might want to put the process to sleep for a while - say, 1/10 sec. Again, it might not be possible for the program to continue anyway - Linux, for example, has the 'OOM killer' which can be configured by an admin.