vc++中的c++问题

n5
博客专家认证
2003-02-05 05:10:47
我用vc++编译一个小程序如下:
#include <iostream.h>
#include <stdlib.h>
#include <new.h>

void OutOfMemory()
{
cerr<<"Unable to staisfy request for memory.\n" ;
abort() ;
}


int main()
{
set_new_handler(OutOfMemory) ;
char *p=new char[10000000000] ;


delete [] p ;

return 0 ;
}

运行结果是:
Assertion failed: new_p == 0, file setnewh.cpp, line 52

abnormal program termination

而如果我改用 _set_new_handler,
则可以正常显示ceer中的信息。

vc++的_set_new_handler和c++标准的set_new_handler除了在参数上不同之外,在其内部的处理上有什么不一样呢?
...全文
66 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
寂寞漂泊 2003-02-15
  • 打赏
  • 举报
回复
看看setnewh.cpp文件中怎么说的吧
寂寞漂泊 2003-02-15
  • 打赏
  • 举报
回复
set_new_handler is a stub function that is provided to
allow compilation of the Standard Template Library (STL).

Do NOT use it to register a new handler. Use _set_new_handler instead.

However, it can be called to remove the current handler:

set_new_handler(NULL); // calls _set_new_handler(NULL)
hellwolf 2003-02-14
  • 打赏
  • 举报
回复
#include <new>

using std::set_new_handler;

我记得以前碰到过的,现在不用了,太破了
n5 2003-02-14
  • 打赏
  • 举报
回复
最后一次up!!!!!!!!!!!!!!!!
n5 2003-02-14
  • 打赏
  • 举报
回复
请解释程序运行的结果
为什么是:Assertion failed: new_p == 0, file setnewh.cpp, line 52

谢谢!
国洋建 2003-02-14
  • 打赏
  • 举报
回复
在new.h中:
#ifndef _MSC_EXTENSIONS
#include <new>
#endif
。。。
。。。
#ifdef _MSC_EXTENSIONS
typedef void (__cdecl * new_handler) ();
_CRTIMP new_handler __cdecl set_new_handler(new_handler);
#endif


在new中:
new_handler __cdecl set_new_handler(new_handler) _THROW0();
国洋建 2003-02-14
  • 打赏
  • 举报
回复
_PNH _set_new_handler( _PNH pNewHandler );

void set_new_handler( NULL );
n5 2003-02-09
  • 打赏
  • 举报
回复
up!!
nothing707 2003-02-09
  • 打赏
  • 举报
回复
共同期待.net2003吧
happyfire 2003-02-09
  • 打赏
  • 举报
回复
再up^^^^
n5 2003-02-07
  • 打赏
  • 举报
回复
up
happyfire 2003-02-06
  • 打赏
  • 举报
回复
up^^^^^^
garbriel 2003-02-06
  • 打赏
  • 举报
回复
use the Borland C++ Builder, that's the standard c++ !
n5 2003-02-06
  • 打赏
  • 举报
回复
_set_new_handler当然执行到OutOfMemory了阿!
abnormal program termination就是执行abort的结果。
(_set_new_handler是vc++的,set_new_handler才是标准的)

to oustar:
我不是用vc++来测试。我只是想知道,微软既然提供这个函数,就一定有些目的。
taowen2002 2003-02-06
  • 打赏
  • 举报
回复
用别的试试看,gcc, vc7。vc6不是很规范
dzhcheng 2003-02-06
  • 打赏
  • 举报
回复
up
n5 2003-02-06
  • 打赏
  • 举报
回复
我查过MSDN了,
没有啊。
(msdn能解决的问题,我是不会来csdn解决的)
rivershan 2003-02-06
  • 打赏
  • 举报
回复
建议去查查MSDN~
VC6中不全是标准C++~
n5 2003-02-06
  • 打赏
  • 举报
回复
没人知道吗?
sunjmmeishao1 2003-02-05
  • 打赏
  • 举报
回复
Assertion failed: new_p == 0, file setnewh.cpp, line 52
这应该是vc现实的错误。可能执行不到OutOfMemory,就先是错误退出了。
_set_new_handler是标准c++,则可以执行到OutOfMemory。
加载更多回复(1)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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