学习中遇到的问题-关于set_new_handler()
来源:互联网 发布:网络金融招聘 编辑:程序博客网 时间:2024/05/21 06:30
关于set_new_handler()
#include <iostream>
#include <new.h>
#include <new.h>
using namespace std;
int nomorememory(size_t size)
{
cout << "Allocation failed. Coalescing heap." << endl;
{
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;
}
{
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函数。
- 学习中遇到的问题-关于set_new_handler()
- 关于在vc6.0中使用set_new_handler的问题
- 关于 set_new_handler的总结
- 关于 set_new_handler的总结
- 《STL源码剖析》中关于set_new_handler的理解
- 关于set_new_handler
- 关于set_new_handler
- 关于set_new_handler
- 关于set_new_handler
- 关于学习编程中会遇到的一些小问题
- C++学习中关于cout遇到的一个小问题
- 关于lua学习中遇到的一些个小问题
- 关于第二天学习python过程中遇到的问题
- 关于学习java中遇到瓶颈的问题
- 关于set_new_handler的一些残存疑问
- 学习中遇到的问题
- 学习中遇到的问题
- 关于多线程学习遇到的一些问题
- 看看是什么在改变 WEB (3)... (WEB改变世界, 我们改变WEB)
- 看看是什么在改变 WEB (4)... (WEB改变世界, 我们改变WEB)
- 泛型
- 日记8.24.2007
- 转的
- 学习中遇到的问题-关于set_new_handler()
- 变更控制的好与坏
- Direct3D 10转型时代所面临的空前窘境
- 配置Windows Server 2003网络负载均衡
- vs 2008中使用Asp.net Ajax智能感知Intellisense
- 一切从头开始
- 高质量C++及C编程指南.doc
- 麻烦了,我没QQ了
- 保护您的 J2ME/MIDP 应用程序