Usage of set_new_handler

Hi,

I have a piece of code that looks something like this.

***********************************************************

#include<new>
#include<iostream>

using namespace std;

static char * memoryReserve=0;

void newhandler( )
{
cout << "The new_handler is called:" << endl;
delete []memoryReserve;
memoryReserve=0;

cout << "Out of memory..Hence exiting";
exit(1);
}

int main( )
{
memoryReserve = new char[10000];
set_new_handler (&newhandler);
//Do something
}


***********************************************************

The purpose of using set_new_handler is understood. It calls newHandler() if operator new cannot allocate any dynamic memory.

newHandler() ideally should log "No memory"error message and exit.

What I dont understand is reserving memory before setting the handler.
And then freeing the memory reserved once handler is called.

Is the above code OK? Could anyone explain the usage of memoryReserve ?

Thanks,
Vijayasri
Well, you COULD just bash your * key before and after code... or you COULD just use the 'Insert code' button.
Topic archived. No new replies allowed.