c++ 内存的例子

// 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

你可能感兴趣的:(C++,c,C#)