The following program segment shows how you could use set_new_
handler() to return a message if the new operator cannot allocate storage:
#include <iostream.h>
#include <new.h>
void no_storage()
{
cerr << "Operator new failed: no storage is available.\n";
exit(1);
}
main()
{
set_new_handler(&no_storage);
// Rest of program ...
}
If the program fails because new cannot allocate storage, the
program exits with the message:
Operator new failed: no storage is available.