3 回答
TA贡献1859条经验 获得超6个赞
第二部分
operator newnew_handler 必
提供更多内存:
安装不同的新处理程序:set_new_handler
卸载新的处理程序:set_new_handleroperator newstd::bad_alloc
抛出异常std::bad_allocoperator new
不返回:abortexit.
new_handlerset_new_handleroperator newset_new_handlerset_new_handleroperator new
new_handler & set_new_handler
要求4(增强):operator newnulloperator new
第3.7.4.1.3节:
未能分配存储的分配函数可以调用当前安装的 new_handler(18.4.2.2(如有的话)。[注意:程序提供的分配函数可以获得当前安装的地址。 new_handler使用 set_new_handler功能( 18.4.2.3)]如果使用空异常声明的分配函数-规范( 15.4),throw()无法分配存储,它将返回一个空指针。未分配存储的任何其他分配函数只能通过抛出类的异常来指示失败。 std::bad_alloc(18.4.2.1)或派生自 std::bad_alloc.
new operator:
void * operator new(std::size_t size) throw(std::bad_alloc){
// custom operator new might take additional params(3.7.3.1.1)
using namespace std;
if (size == 0) // handle 0-byte requests
{
size = 1; // by treating them as
} // 1-byte requests
while (true)
{
//attempt to allocate size bytes;
//if (the allocation was successful)
//return (a pointer to the memory);
//allocation was unsuccessful; find out what the current new-handling function is (see below)
new_handler globalHandler = set_new_handler(0);
set_new_handler(globalHandler);
if (globalHandler) //If new_hander is registered call it
(*globalHandler)();
else
throw std::bad_alloc(); //No handler is registered throw an exception
}}- 3 回答
- 0 关注
- 414 浏览
添加回答
举报
