C++中的set_new_handler函数

首先,namespace std中有如下定义:

  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

----------------------------------------------------------------------------------------------------------------------------------------------------------

译文开始:对于函数set_new_handler

函数说明

1.   set_new_handler函数的作用是设置new_p指向的函数为new操作或new[]操作失败时调用的处理函数。

2.   设置的处理函数可以尝试使更多空间变为可分配状态,这样新一次的new操作就可能成功。当且仅当该函数成功获得更多可用空间它才会返回;否则它将抛出bad_alloc异常(或者继承该异常的子类)或者终止程序(例如调用abortexit)。

3.   如果设置的处理函数返回了(例如,该函数成功获得了更多的可用空间),它可能将被反复调用,直到内存分配成功,或者它不再返回,或者被其它函数所替代。

4.   在尚未用set_new_handler设置处理函数,或者设置的处理函数为空时,将调用默认的处理函数,该函数在内存分配失败时抛出bad_alloc异常。

参数说明

new_p:该函数指针所指的函数应为空参数列表且返回值类型为void

该函数可以尝试获得更多的可用空间,或者抛出异常,或者终止程序。

如果是一个空指针,处理函数将被重置为默认值(将会执行抛出bad_alloc异常)。

返回值

返回先前被设置的处理函数指针;如果尚未被设置或者已被重置,将返回空指针。

返回的函数指针是无参的void返回值类型的函数指针。

下面是一个简单的示例程序:

// 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;
}

new操作分配内存失败时将调用no_memory函数。

数据争用:

调用此函数不会引入数据竞争,任何这样的调用将会和随后set_new_handlerset_new_handler的调用同步。

注意,此要求仅适用于set_new_handler函数,但对于作为参数(new_p)传递的新处理函数却非必须。

异常安全:

无异常抛出保证:该函数(set_new_handler)不会抛出异常。

注意,如果new_p是没有实现适当功能的函数指针(见上面的参数说明),或者如果new_p是无效的指针,它会导致未定义的行为。

---------------------------------------------------------------------------------------------------------------------------------------------------------

原文如下:

Set new handler function

Sets new_p as the new-handlerfunction.

The new-handler function is a function which is called by thedefault allocation functions (operatornew and operatornew[]) when they fail to allocate storage.

The new-handler function may try to make more storageavailable for a new attempt to allocate the storage. If -and only if- thefunction succeeds in making more storage available, it may return. Otherwise itshall either throw a bad_allocexception (or a derived class) orterminate the program (such as by calling abort or exit).

If the new-handler function returns (i.e., it made morestorage available), it may be called repeatedly for as long as theallocationfunction fails to allocate the requested storage, or until the new-handlerfunction does not return or is replaced.

Before this function is called by the program for the first time, or if new_p isa null-pointer, the default allocation functions directlythrow bad_alloc on failure.


Parameters

new_p

Functionthat takes no arguments and returns no value (void).
The function can make more storage available, or throw an exception, orterminate the program.
If this is a null-pointer, the new-handler function isreset to none (and bad_alloc is thrown instead).
new_handler isa function pointer type for functions that take no arguments and return novalue.



Return value

The value ofthe current new-handler function if this has already been setby this function previously, or a null-pointer if this is thefirst call to set_new_handler (or if it was reset by a previouscall).

new_handler isa function pointer type taking no arguments and returning no value.

Data races

Calling this function does notintroduce data races, and any such call is synchronized with subsequent callstoset_new_handler and get_new_handler.

Notice that this requirement applies only to the set_new_handler function,but not necessarily to the new-handler functionpassed as argument (new_p).


Exception safety

No-throwguarantee: this function (set_new_handler) never throwsexceptions.

Notice that if new_p is a function that does not implement theproper functionality (described above), or if new_p is aninvalid pointer, it causes undefined behavior.



你可能感兴趣的:(C++中的set_new_handler函数)