GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::set_new_handler(3) C++ Standard Libary std::set_new_handler(3)

std::set_new_handler - std::set_new_handler


Defined in header <new>
std::new_handler set_new_handler( std::new_handler new_p ) throw(); (until C++11)
std::new_handler set_new_handler( std::new_handler new_p ) noexcept; (since C++11)


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
2) terminate the program (e.g. by calling std::terminate)
3) throw exception of type std::bad_alloc or derived from std::bad_alloc.


The default implementation throws std::bad_alloc. The user can install his 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 (since C++11)
std::set_new_handler and std::get_new_handler calls.


new_p - pointer to function of type std::new_handler, or null pointer


The previously-installed new handler, or a null pointer value if none was installed.

// Run this code


#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';
}
}


Memory allocation failed, terminating
std::bad_alloc


operator new allocation functions
operator new[] (function)
get_new_handler obtains the current new handler
(C++11) (function)
new_handler function pointer type of the new handler
(typedef)
bad_alloc exception thrown when memory allocation fails
(class)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.