...

View Full Version : set_new_handler error



adarshakb
07-17-2009, 08:45 PM
this is my code

#include <iostream.h>
#include <stdlib.h>
#include <new.h>



void main()
{
void memFull();
set_new_handler(memFull);

int z[6];
cout<<&z;
}
void memFull()
{
cout<<"Couldnt allocate memory as memory full ";
}


There is a runtime error in setting up set_new_handler
i cant figure out why


Another code tat again has problem with set_new_handler


#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <new.h>

void memFull()
{
cout<<"MEMORY OVERFLOW";
exit(-1);
}


void* operator new(size_t,int);
void operator delete(void*);


void main()
{

set_new_handler(memFull);
int *p=new(5) int[10];


for(int i=0;i<10;i++)
cout<<p[i]<<"\n";

delete p;
}

void* operator new(size_t size,int num)
{
void* p;
p=malloc(size);

if(p==NULL)
memFull();

memset(p,num,size);

return p;
}

void operator delete(void* p)
{
free(p);
}



And HOW to give set_new_handler function functions with parameters... or is there a no-parameters rule :D

BrickInTheWall
07-17-2009, 08:51 PM
if you are going to define the function after main, you'll have to declare a prototype of the function before main or in one of the header files.

adarshakb
07-17-2009, 08:56 PM
i have declared the prototype in the 1st line of main...

we can declare there or before main...
still the bug persists at the set_new_handler(memFull) line :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum