delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/26/15:29:02

Message-Id: <m0zBkWV-000S4TC@inti.gov.ar>
Comments: Authenticated sender is <salvador AT natacha DOT inti DOT gov DOT ar>
From: "Salvador Eduardo Tropea (SET)" <salvador AT inti DOT gov DOT ar>
Organization: INTI
To: arfa AT clara DOT net, djgpp AT delorie DOT com
Date: Wed, 26 Aug 1998 15:43:38 +0000
MIME-Version: 1.0
Subject: RE: Problem with "new"
In-reply-to: <000001bdd112$07f8bc00$214e08c3@arthur>
References: <35e404cf DOT 33475 AT news DOT space DOT net>

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


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019