可以看这里:

http://blog.csdn.net/huyiyang2010/article/details/5984987

 

现在的new是会抛出异常的,bad::alloc

如果不想抛出异常两种方法:

1. 用nothrow版本, new (std::nothrow) xxx();

那样new失败会返回NULL

 

2. 加一个new_handler

但是注意不要直接改全部的,最好在自定义的operator new里面来加:

在operator new中做如下事情:

1、首先调用标准的set_new_handler,自定义专属类的处理函数

2、调用global operator new,执行实际的内存分配。如果内存分配失败,刚才被安装的new_handler将被调用。

3、无论new成功还是失败,都必须在类自定义的operator new结束前恢复全局new_handler

 

相关文章:

  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2022-12-23
  • 2021-04-02
  • 2021-11-26
  • 2021-10-09
相关资源
相似解决方案