Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/new/set new handler"

From cppreference.com
< cpp‎ | memory‎ | new
(remove stray ==notes==)
(fmt)
Line 44: Line 44:
 
#include <iostream>
 
#include <iostream>
 
#include <new>
 
#include <new>
 +
 
void handler()
 
void handler()
 
{
 
{
Line 49: Line 50:
 
     std::set_new_handler(nullptr);
 
     std::set_new_handler(nullptr);
 
}
 
}
 +
 
int main()
 
int main()
 
{
 
{
 
     std::set_new_handler(handler);
 
     std::set_new_handler(handler);
 
     try {
 
     try {
         while(true)
+
         while (true) {
 
             new int[100000000ul];
 
             new int[100000000ul];
     }catch(const std::bad_alloc& e) {
+
        }
 +
     } catch (const std::bad_alloc& e) {
 
         std::cout << e.what() << '\n';
 
         std::cout << e.what() << '\n';
 
     }
 
     }

Revision as of 10:45, 23 September 2013

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
 
Defined in header <new>
std::new_handler set_new_handler( std::new_handler new_p )

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.

Contents

Parameters

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

Return value

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

Exceptions

(none) (until C++11)
noexcept specification:  
noexcept
  
(since C++11)

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[100000000ul];
        }
    } catch (const std::bad_alloc& e) {
        std::cout << e.what() << '\n';
    }
}

Output:

Memory allocation failed, terminating
std::bad_alloc

See also

allocation functions
(function) [edit]
set_new_handler
registers a new handler
(function) [edit]
function pointer type of the new handler
(typedef) [edit]
exception thrown when memory allocation fails
(class) [edit]