Mail Archives: djgpp/1998/08/26/15:29:02
<arfa AT clara DOT net> wrote:
> > I use gcc version 2.7.2.1 and I try to reserve some memory with "new".
> > My problem is, if there is not enough memory the program returns to
> > the OS (code 255), what I don't want.
> > How can I avoid this?
> >
> > tia...
> > ...Klaus...
> >
> >
>
> new returns NULL if memory allocation fails. You should always test for this.
>
> eg:
>
> int *a;
> a = new int;
> if(!a){ do_something() }
>
> I think this should work.
At least in 2.7.2 that isn't true and new aborts with a Virtual memory
exhausted message. You can hook a function that will be called when new
fails.
I use in my editor:
// Taked from the GCC 2.7.2.1 sources
typedef void (*vfp)(void);
extern "C" vfp set_new_handler(vfp handler);
static void *safetypool;
static void mynewhandler(void)
{
if (safetypool==NULL)
_exit(1); // We can't call exit because it can need malloc
delete [] safetypool;
safetypool=NULL;
set_new_handler(NULL);
messageBox(_("Memory is nearly full. Please exit, and restart."), mfOKButton
| mfError);
}
And in main:
safetypool=new char [64000];
set_new_handler (mynewhandler);
Note that it doesn't work with the normal malloc routine from v2.01.
It compiles OK with gcc 2.8.0.
SET
------------------------------------ 0 --------------------------------
Visit my home page: http://set-soft.home.ml.org/
or
http://www.geocities.com/SiliconValley/Vista/6552/
Salvador Eduardo Tropea (SET). (Electronics Engineer)
Alternative e-mail: set-soft AT usa DOT net set AT computer DOT org
ICQ: 2951574
Address: Curapaligue 2124, Caseros, 3 de Febrero
Buenos Aires, (1678), ARGENTINA
TE: +(541) 759 0013
- Raw text -