set_new_handler的问题

lookingfor 2005-06-24 06:25:16
#include <iostream>
#include <new>
using namespace std;

void _cdecl newhandler()
{
cerr<<"error to alloc memory"<<endl;
throw(std::bad_alloc());
}

int main()
{
set_new_handler(newhandler);
new int[10000];
return 0;
}
编译时无错
运行时VC6下出错,即使不做new int[10000]这一步也是如此.
而VS.NET2003下则可正常处理,抛出异常.
请问这是为什么?
...全文
87 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
foochow 2005-06-24
  • 打赏
  • 举报
回复
vc6.0的set_new_handle只是把new的处理函数置零,要用_set_new_handle;还有new.h不在STL的名字空间中,所以即使用了using namespace std;也不能去掉.h;
.......................
_set_new_handler的原型:
_PNH _set_new_handler( _PNH pNewHandler );
_PNH, defined in NEW.H, is a pointer to a function that returns type int and takes an argument of type size_t. Use size_t to specify the amount of space to be allocated.
....................................................
#include <iostream>
#include <new.h>
using namespace std;
int newhandler(size_t size)
{
cerr<<"error to alloc memory"<<endl;
return 0;
}
int main()
{
_set_new_handler(newhandler);
new int[99999999999999999];
return 0;
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧