学习中遇到的问题-关于set_new_handler()
2007-08-25 14:09
549 查看
关于set_new_handler()
#include <iostream>
#include <new.h>
using namespace std;
int nomorememory(size_t size)
{
cout << "Allocation failed. Coalescing heap." << endl;
return 0;//try return 0;
}
void nomorememory1()
{
cout << "Allocation failed. Coalescing heap." << endl;
}
int main(int argc, char* argv[])
{
_set_new_handler(nomorememory);
try{
int *pbigdataarray = new int[1000000000];
if(pbigdataarray==NULL)
cout<<"wrong"<<endl;
}
catch(bad_alloc a)
{
cout<<"test1"<<endl;
}
catch(...)
{
cout<<"test2"<<endl;
}
cout<<"finish"<<endl;
return 0;
}
/***************/
问题:
首先在vc6.0(xp)下,使用set_new_handler(nomorememory1);运行时出现assert错误。查看set_new_handler源码发现有assert(new_p==0).说明该函数只能这样用set_new_handler(0)。这是在VC++中得到的结果,这说明在使用的编译器中没有实现标准规定的 set_new_handler()函数。
故采用_set_new_handler(nomorememory)替换set_new_handler。其中nomorememory函数的参数size为申请的内存的大小。nomorememory函数的返回值如果0则表明分配失败,如果非0则表明应该抽取分配的内存。当new发现nomorememory返回0,则new返回0. 当new发现nomorememory返回非0,则new抽取分配的内存。
在实际使用中发现,如果nomorememory返回非0,当new无法满足内存需求时,它会不只一次的调用nomorememory函数, 它会不断的调用,直道找到足够的内存。如果nomorememory返回0,则new返回0,并且只调用一次nomorememory函数。
相关文章推荐
- 关于在vc6.0中使用set_new_handler的问题
- 分享关于学习new BufferedWriter()方法时常遇到的一个无厘头的问题
- 学习AOP时遇到关于InvocationHandler接口的问题
- 关于hadoop2.4.2版本学习时遇到的问题
- 我遇到了第一个问题,关于学习asp.net的基础
- 关于set_new_handler
- 关于学习编程中会遇到的一些小问题
- 最近在ArcGIS Engine开发中关于调用gp工具过程出现COM 组件的调用返回了错误 HRESULT E_FAIL 错误的解决方法 和 学习oracle中遇到的一些问题总结
- 关于UCOS学习OSMboxCreate(NULL)的问题,希望遇到同样问题的人得到合理的解答
- 关于 set_new_handler的总结
- 关于set_new_handler(转载)
- 关于Android开发导入已有项目遇到Project has no default.properties file! Edit the project properties to set one.问题的解决方法
- Android 开发学习手记(三):关于PullToRefresh的setOnTouchListener()无效的问题。
- 关于set_new_handler
- 关于Handler使用遇到的问题--多执行了些循环。
- QT 学习中遇到的问题~~~~常见的信号处理机制(on_browseButton_clicked()) (toLower) (separator()) (setText)
- 关于set_new_handler(转载)
- C++ 关于static variables的学习中遇到的问题总结
- 关于学习swift遇到的问题吧3-26
- 今天又学习了,关于windows和Linux双系统删除Linux遇到的问题。