c++ 内存的例子

本文通过一个C++程序示例展示了如何使用new操作符不断尝试分配内存,并在内存不足时调用自定义的新处理函数。当无法分配更多内存时,程序会抛出异常并捕获处理。
// Win32MemoryTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <limits>
#include <iostream>
using namespace std;
void noMoreMemory()
{
	std::cerr<<"noMoreMemory"<<endl;
}
class X
{
	int x;
	int y;
	int z;
};
class Y
{

};
int _tmain(int argc, _TCHAR* argv[])
{
	set_new_handler(noMoreMemory);
	try
	{
		while(true)
		{
			X* p1 = new X[100000000];
		}
		
	}
	catch(std::exception& bad)
	{
		cout<<bad.what()<<std::endl;
		exit(0);
	}

	return 0;
}

 可以看到,不断地进入了handle里面。也就是说,如果找不到内存,就不断地去调用handle

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值