Effective C++实践
来源:互联网 发布:win10电脑无法识别网络 编辑:程序博客网 时间:2024/06/07 15:32
在C++编程中,new是可能会造成异常的,噢列个去,刚学习到这儿,不禁汗流浃背,以前写代码哪注意到这?!了解完C++的特点,才发现C++真的到处是坑爹的坑...
解决方法是使用set_new_handler函数调用客户定义的new_handler函数。书上网上都找了些例子,可总是编译不过,老是提示参数不对。想明白了,其实就是返回值定义错误。<new>中的定义如下:
typedef void (*new_handler)();
new_handler set_new_handler(new_handler) throw();
所以示例代码应该这样写:
main.cpp文件
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
/// 此处为std::new_handler 而非void
std::new_handler noMoreMemory()
{
cerr << "Unable to satisfy request for memory\n";
abort();
}
int main()
{
set_new_handler(noMoreMemory());
int *pBigArray = new int[100000000];
delete pBigArray;
return 0;
}
现在真对以前写的代码抱有罪恶感了
- Effective C++实践
- 《Effective C++》和《More Effective C++》汇总
- 《Effective C++》读书笔记
- 《Effective C#》 翻译札记
- 《Effective C++》读感
- 《Effective C++》读后感
- 读《Effective C++》所想
- 《Effective C++》读书笔记
- 《Effective C#》 翻译札记
- 细读《Effective C++》之一
- 《Effective C#》 翻译札记
- effective c sharp 印象
- 细读《Effective C++》之一
- Begin reading 'Effective c++'
- 《Effective C#》 翻译札记
- 《Effective c++》读书笔记
- 《Effective C++》 笔记
- Effective C++(1-4)
- [Leetcode] Binary Tree Level Order Traversal
- cocos2d-x用rube box2d editor当关卡编辑器写游戏
- 几种进程间的通信方式
- 网络红人潘春春写真曝光 因尺度大胆走红 秒杀宅男女神柳岩林志玲
- C 宏定义
- Effective C++实践
- json & bosn
- Saving and Loading the Checklist Items
- 浅谈C++中的友元关系
- 成功申请到ClickDealer联盟
- 对于学习效率的感想
- 测试你最真实的一面!很准的哦
- SSE 指令 gcc编译
- UNDO管理