Makes new_p the new global new-handler function and returns the previously installed new-handler.
The new-handler function is the function called by allocation functions whenever a memory allocation attempt fails. Its intended purpose is one of three things:
1) make more memory available,
The default implementation throws std::bad_alloc. The user can install their own new-handler, which may offer behavior different than the default one.
If new-handler returns, the allocation function repeats the previously-failed allocation attempt and calls the new-handler again if the allocation fails again. To end the loop, new-handler may call std::set_new_handler(nullptr): if, after a failed allocation attempt, allocation function finds that std::get_new_handler returns a null pointer value, it will throw std::bad_alloc.
At program startup, new-handler is a null pointer.
This function is thread-safe. Every call to std::set_new_handler
synchronizes-with (see std::memory_order) the subsequent std::set_new_handler
and std::get_new_handler calls.
The previously-installed new handler, or a null pointer value if none was installed.
[edit] Example#include <iostream> #include <new> void handler() { std::cout << "Memory allocation failed, terminating\n"; std::set_new_handler(nullptr); } int main() { std::set_new_handler(handler); try { while (true) { new int[1000'000'000ul](); } } catch (const std::bad_alloc& e) { std::cout << e.what() << '\n'; } }
Possible output:
Memory allocation failed, terminating std::bad_alloc[edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4