Just as you can use Atexit to register the exit handler function for main, we can also use Std::set_new_handler to register the error handler for operator new.
Operator new throws an exception when operator new fails to meet a memory allocation requirement.
However, when operator new does not meet a memory allocation requirement and throws an exception, a user-specified error-handling function, called New-handler, is invoked first.
That is, operator new does not meet the memory allocation requirement---> (loop) call executes the user-specified error handler New-handler--->operator new throws an exception
The user can call the Std::set_new_handler interface to specify this "to handle out-of-memory" function New-handler,
New-handler is a standard library function declared in <new>, as follows:
Namespace Std {
typdef void (*new_handler) ();
New_handler Set_new_handler (New_handler p) throw ();
}
Know:
1 New_handler is a pointer to a function defined by a typedef that does not return anything
2) Set_new_handler: A function that obtains a new_handler and returns a New_handler
The implementation of the Set_new_handler function is probably as follows:
New_handler Set_new_handler (New_handler p) throw
{new_handler Oldhandler
= M_curhandler;
M_curhandler = p;
return oldhandler;
}
3 A declarative End throw () is an exception detail indicating that the function does not throw any exceptions
Note: When operator new does not meet the memory request, operator new calls the New-handler function repeatedly until enough memory is found.
Examples are as follows:
#include <stdlib.h> //for abort ()
#include <iostream>
void Outofmem ()
{
Std::cerr << "operator new unable to satisfy request for memory, try allocate memory again.\n";
int main ()
{
std::set_new_handler (OUTOFMEM);
int *buf = new int[1000000000l];
Delete []buf;
return 0;
}
The test results are as follows:
The above tests show that when operator new is unable to meet the memory allocation, if you std::set_new_handler set operator new's error-handling function New_handler, you will repeat the call to New_ handler function until enough memory is found (if you call abort or exit within New_handler, New_handler will only be invoked once).
With further conclusions, it is important to design a good new_handler function.
It is reasonable and proper that New_handler can choose one of the following ways to deal with the problems.
Processing Mode 1: Let more memory be used
At the beginning of the program, it applies for allocating a large chunk of memory, releasing it when New_handler is called and then using the program.
Treatment Mode 2: Install another New_handler
If the new_handler1 cannot be processed, the Set_new_handler settings New_handler2 are invoked inside the new_handler1, and the next time the New_handler2 is processed.
Treatment Mode 3: Uninstall New_handler
Std::set_new_handler (NULL) to unload the New_handler, operator new does not invoke the error-handling function New_handler when the memory allocation fails and throws a different std::bad_ directly Alloc and abort off the program.
Handling Mode 4: Throws Std::bad_alloc or derives from Std::bad_alloc exceptions.
The exception is not captured by operator new, so it is propagated to the memory claim.
Treatment Mode 5: Call abort or exit
The following are some of the five ways to encode.
Treatment Mode 2: Install another New_handler
If the new_handler1 cannot be processed, the Set_new_handler settings New_handler2 are invoked inside the new_handler1, and the next time the New_handler2 is processed.
#include <stdlib.h> //for abort ()
#include <iostream>
void handle_outofmem2 ()
{
std::cerr<<__function__<< ": operator new unable to satisfy request for memory, try allocate memory again.\n"; c5/>}
void Handle_outofmem ()
{
std::cerr<<__function__<< ": operator new unable to satisfy Request for memory, try allocate memory again.\n ";
Std::set_new_handler (HANDLE_OUTOFMEM2);
}
int main ()
{
std::set_new_handler (HANDLE_OUTOFMEM);
int *buf = new int[1000000000l];
delete []buf;
return 0;
}
The test results are as follows:
Treatment Mode 3: Uninstall New_handler
Std::set_new_handler (NULL) to unload the New_handler, operator new does not invoke the error-handling function New_handler when the memory allocation fails.
#include <stdlib.h> //for abort ()
#include <iostream>
void Handle_outofmem ()
{
std ::cerr<<__function__<< ": operator new unable to satisfy request for memory, try allocate memory again.\n";
std::set_new_handler (NULL);
}
int main ()
{
std::set_new_handler (HANDLE_OUTOFMEM);
int *buf = new int[1000000000l];
delete []buf;
return 0;
}
The test results are as follows:
Treatment Mode 5: Call abort or exit
#include <stdlib.h> //for abort ()
#include <iostream>
void Handle_outofmem ()
{
std::cerr<< "operator new allocate memory failed, try again.\n";
Abort ();
}
int main ()
{
std::set_new_handler (HANDLE_OUTOFMEM);
int* buf = new int[1000000000000000l];
delete []buf;
return 0;
}
The test results are as follows:
In C + + development projects, the general may need to register the specific class of custom New_handler, can refer to the "Effective C + + improvement procedures and design of the 55 specific practices."
Finish