Message-Id: Comments: Authenticated sender is From: "Salvador Eduardo Tropea (SET)" 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 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: RE: Problem with "new" In-reply-to: <000001bdd112$07f8bc00$214e08c3@arthur> References: <35e404cf DOT 33475 AT news DOT space DOT net> Precedence: bulk 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