new_handler set_new_handler (new_handler new_p) throw();
new_handler set_new_handler (new_handler new_p) noexcept;
void).1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// new_handler example
#include <iostream> // std::cout
#include <cstdlib> // std::exit
#include <new> // std::set_new_handler
void no_memory () {
std::cout << "Failed to allocate memory!\n";
std::exit (1);
}
int main () {
std::set_new_handler(no_memory);
std::cout << "Attempting to allocate 1 GiB...";
char* p = new char [1024*1024*1024];
std::cout << "Ok\n";
delete[] p;
return 0;
}
Attempting to allocate 1 GiB... Ok