Use of C++ memory allocation handler set_new_handler

  • 2020-07-21 09:36:44
  • OfStack

1. Function definition

The function in namespace std is defined as follows (C++98 is not identical to C++11) :


Typedef void (*new_handler)();
 
new_handler set_new_handler(new_handler new_p) throw();  //C++98
new_handler set_new_handler (new_handler new_p) noexcept; //C++11

2. Function introduction

The new_p function is called when the new operation or new[] operation fails

Abnormal safety:

C++98 and C++11 use throw() and noexcept () declarations after the function, so the function (set_new_handler) does not throw an exception Note: new_p results in undefined behavior if it is a function pointer that does not implement the appropriate functionality (see the argument below), or if new_p is an invalid pointer

Data contention:

Calling this function does not introduce data contention and any such call will be synchronized with subsequent calls to set_new_handler and set_new_handler Note that this requirement applies only to the set_new_handler function, but is not required for the new handler passed as an argument (new_p)

Function description

1. The set_new_handler function sets the function pointed to by new_p to be the handler called when the new operation or new[] operation fails.

2. The handler set can attempt to make more space allocable so that the new new operation may succeed. This function returns if and only if it succeeds in gaining more free space; Otherwise it throws an bad_alloc exception (or a subclass of the exception) or terminates the program (such as calling abort or exit).

3. If a handler is set that returns (for example, the function successfully gains more free space), it may be called repeatedly until memory allocation is successful, or it no longer returns, or it is replaced by another function.

4. When the handler is not set with set_new_handler, or is set to null, the default handler is called, which throws an bad_alloc exception when memory allocation fails.

3. Parameters of the function
new_p:

A function called when the new operation or new[] operation fails The function takes an empty argument list and returns a value of type void This function can try to get more free space, either throw an exception, or terminate the program If it is 1 null pointer or 0, the handler is reset to the default (an bad_alloc exception is thrown)

The handler set can attempt to make more space allocates so that the new new operation may succeed. This function returns if and only if it succeeds in gaining more free space; Otherwise it throws an bad_alloc exception (or a subclass of that exception) or terminates the program (such as calling abort or exit)

If a handler is set that returns (for example, the function succeeds in gaining more free space), it may be called repeatedly until memory allocation is successful, or it no longer returns, or it is replaced by another function

If the handler is not set, or if the handler is set to be empty, the default handler is called, which throws an bad_alloc exception when memory allocation fails

The return value of the function

Returns the handler pointer that was set earlier If the set_new_handler parameter is null or has been reset, a null pointer is returned The function pointer returned is parameterless with a return value of type void

5. Demonstrate your case

The no_memory function is called in the following program if the new operation fails to allocate memory


// 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 () {
  // The binding no_memory The processing function 
  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;
}

Related articles: