C++ operator new when out of heap.

Nick.SIMON at syntegra.bt.co.uk Nick.SIMON at syntegra.bt.co.uk
Thu Aug 31 08:42:35 UTC 2000


You can use the nothrow version of new (you may have to include new or
new.h):

    SomeClass * anObject = new(nothrow) SomeClass;
    if(NULL == anObject)
    {
        handleBadAlloc();
    }

It may be there's some compile option or pragma that will make the nothrow
version the default.  Otherwise people whose pre-new-exception code relied
on doing smart things on out-of-memory would have had to do lots of work
when compilers for the new standard came in.  Does anyone on the list know
of this?


BTW I don't blame you not using exceptions on an embedded system, there's a
lot of overhead.  Unfortunately we're more or less committed to it :(

-- Nick Simon 

> -----Original Message-----
> From: angelo [mailto:angelo at hunterlink.net.au]
> Sent: 31 August 2000 02:27
> To: Nick.SIMON at syntegra.bt.co.uk
> Subject: Re: C++ operator new when out of heap.
> 
> 
> You are right, that is what the standard says. I suppose this 
> means I'll have
> to overload operator new in all my classes so they will 
> return NULL, and thus
> not break my old code;
> 
> 
> 
> Nick.SIMON at syntegra.bt.co.uk wrote:
> 
> > C++ Programming Language, 3rd edition bu Bjarne Stroustrup 
> (and he should
> > know) says "What happens when new can find no store to 
> allocate? By default
> > the operator throws a bad_alloc exception." (section 6.2.6.2).
> >
> > The 1996 draft standard at 
> http://anubis.dkuug.dk/jtc1/sc22/open/n2356/ or
> > at ftp://research.att.com/dist/c++std/WP/CD2/body.pdf says: 
> "If   a nothrow
> > allocation function (_lib.support.dynamic_) fails to 
> allocate storage, it
> > shall return a null pointer.  Any other allocation function 
> that fails to
> > allocate storage shall only indicate failure by throwing an 
> exception of
> > class std::bad_alloc (_lib.bad.alloc_) or a class derived from
> > std::bad_alloc." in section 3.7.3.1  Allocation functions
> > [basic.stc.dynamic.allocation].
> >
> > I'm too mean to pay for the current standard!
> >
> > My RTEMS Panics on out-of-heap anyway.  Is this a build option?
> >
> > -- Nick Simon
> >
> > > -----Original Message-----
> > > From: angelo [mailto:angelo at hunterlink.net.au]
> > > Sent: 30 August 2000 00:39
> > > To: Nick.SIMON at syntegra.bt.co.uk
> > > Cc: rtems-users at oarcorp.com
> > > Subject: Re: C++ operator new when out of heap.
> > >
> > >
> > > Do you have a reference for this?? I thought the standard was
> > > completed in
> > > 1988.
> > >
> > > Nick.SIMON at syntegra.bt.co.uk wrote:
> > >
> > > > The C++ spec now says that 'new' never returns null, but if
> > > out of heap
> > > > throws an exception (a C++ exception that is).
> > > >
> > > > Does anyone know if this is the case with RTEMS tools &
> > > libraries, or is
> > > > null returned as per the old standard?  I'm using 4.5.0
> > > beta 3 (soon to be
> > > > beta 3a).
> > > >
> > > > TIA,
> > > >
> > > > -- Nick Simon
> > > >
> > > > I used to think I was uncertain, but now I'm not sure.
> > >
> > > --
> > > Angelo Fraietta
> > >
> > > PO Box 859
> > > Hamilton NSW 2303
> > >
> > > Home Page
> > >
> > > http://users.hunterlink.net.au/~maaaf/
> > >
> > > There are those who seek knowledge for the sake of 
> knowledge - that is
> > > CURIOSITY
> > > There are those who seek knowledge to be known by others -
> > > that is VANITY
> > > There are those who seek knowledge in order to serve - 
> that is LOVE
> > >     Bernard of Clairvaux (1090 - 1153)
> > >
> > >
> > >
> 
> --
> Angelo Fraietta
> 
> PO Box 859
> Hamilton NSW 2303
> 
> Home Page
> 
> http://users.hunterlink.net.au/~maaaf/
> 
> There are those who seek knowledge for the sake of knowledge - that is
> CURIOSITY
> There are those who seek knowledge to be known by others - 
> that is VANITY
> There are those who seek knowledge in order to serve - that is LOVE
>     Bernard of Clairvaux (1090 - 1153)
> 
> 



More information about the users mailing list