set_new_handler() - Set Behavior for new Failure

When the new operator creates a new object, it calls the operator new() function to obtain the needed storage.

When new cannot allocate storage, it calls a new handler function if one has been installed by a call to set_new_handler(). The set_new_handler() function is defined in <new.h>. Use it to call a new handler you have defined or the default new handler.

The set_new_handler() function has the prototype:

typedef void(*PNH)();
PNH set_new_handler(PNH);

set_new_handler() takes as an argument a pointer to a function (the new handler), which has no arguments and returns void. It returns a pointer to the previous new handler function.

If you do not specify your own set_new_handler() function, new returns the NULL pointer.

The _set_mt_new_handler() function behaves exactly the same way as set_new_handler(), except that it only affects the current thread. When a new handler function needs to be called, the code first checks for a thread new handler. If one has been registered, it is called. If not, the new handler registered with set_new_handler() is called.



Constructors and Destructors Overview


Example of set_new_handler()


new Operator
Member Functions and the Global operator new()
Initializing Objects Created with the new Operator
Overloaded new and delete
Free Store