#include "stdafx.h"
#include <cstring>
#include <iostream>
void OutOfMenory(){
std::cerr << "unable to satisfy request for memory /n" << std::endl;
std::abort();
}
int _tmain(int argc, _TCHAR* argv[])
{
std::set_new_handler(OutOfMenory);
for (size_t i = 0; i < 10000; i++)
{
std::cout << i << std::endl;
int *p_big_data_array = new int[10000000];
}
return 0;
}
条款49 了解new-handler的行为
最新推荐文章于 2022-07-23 17:53:54 发布
本文通过C++示例代码展示了如何设置全局new handler来处理内存分配失败的情况,并演示了一个简单的循环,每次迭代中都尝试分配大量内存,直至内存耗尽触发异常处理。
562

被折叠的 条评论
为什么被折叠?



