#include<iostream>
//#include<new.h>
#include<malloc.h>
using namespace std;
void out_of_memory()
{
cout<<"Out Of Memory!"<<endl;
//exit(1);
// return -1;
};
typedef void(* new_handler)();
void (* cur_handler)()=0;
void(* my_set_new_handler(void(* p)()))()
{
void (* old)()=cur_handler;
cur_handler=p;
return old;
}
void *operator new(size_t size)
{
if(size==0)
size=1;
while(1)
{
char* p=(char *)malloc(size*64);
if(p!=NULL)
return p;
void (* current)()=my_set_new_handler(0);
my_set_new_handler(current);
if(current!=NULL)
(*current)();
else
throw std::bad_alloc();
}
}
void main()
{
my_set_new_handler(out_of_memory);
int *p = new int[10479149179]; //回调方式 operator new
//int *p = new int[10];
if(p == NULL)
{
cout<<"Out Of Memery."<<endl;
exit(1);
}
cout<<"OK."<<endl;
}
//#include<new.h>
#include<malloc.h>
using namespace std;
void out_of_memory()
{
cout<<"Out Of Memory!"<<endl;
//exit(1);
// return -1;
};
typedef void(* new_handler)();
void (* cur_handler)()=0;
void(* my_set_new_handler(void(* p)()))()
{
void (* old)()=cur_handler;
cur_handler=p;
return old;
}
void *operator new(size_t size)
{
if(size==0)
size=1;
while(1)
{
char* p=(char *)malloc(size*64);
if(p!=NULL)
return p;
void (* current)()=my_set_new_handler(0);
my_set_new_handler(current);
if(current!=NULL)
(*current)();
else
throw std::bad_alloc();
}
}
void main()
{
my_set_new_handler(out_of_memory);
int *p = new int[10479149179]; //回调方式 operator new
//int *p = new int[10];
if(p == NULL)
{
cout<<"Out Of Memery."<<endl;
exit(1);
}
cout<<"OK."<<endl;
}