Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   new throws or returns? (http://www.velocityreviews.com/forums/t459658-new-throws-or-returns.html)

Chameleon 01-05-2007 01:46 PM

new throws or returns?
 
I am confused on this:

When "new" fails, what happens?
- throws bad_alloc
- returns 0

I found many docs with first, many with second and one with both(!) cases.


thanks!

Ondra Holub 01-05-2007 01:53 PM

Re: new throws or returns?
 

Chameleon napsal:
> I am confused on this:
>
> When "new" fails, what happens?
> - throws bad_alloc
> - returns 0
>
> I found many docs with first, many with second and one with both(!) cases.
>
>
> thanks!


When new int[1000000] fails, it throw std::bad_alloc.
When new(std::nothrow)int[1000000] fails, it returns 0


mlimber 01-05-2007 02:01 PM

Re: new throws or returns?
 
Ondra Holub wrote:
> Chameleon napsal:
> > I am confused on this:
> >
> > When "new" fails, what happens?
> > - throws bad_alloc
> > - returns 0
> >
> > I found many docs with first, many with second and one with both(!) cases.
> >
> >
> > thanks!

>
> When new int[1000000] fails, it throw std::bad_alloc.
> When new(std::nothrow)int[1000000] fails, it returns 0


Right, on standard-compliant implementations. There are non-standard
implementations out there that return null on a plain new because they
don't support exceptions or are simply out of date.

Cheers! --M


Rolf Magnus 01-05-2007 02:09 PM

Re: new throws or returns?
 
Chameleon wrote:

> I am confused on this:
>
> When "new" fails, what happens?
> - throws bad_alloc
> - returns 0
>
> I found many docs with first, many with second and one with both(!) cases.


The first happens.

Roland Pibinger 01-05-2007 08:28 PM

Re: new throws or returns?
 
On Fri, 05 Jan 2007 15:46:42 +0200, Chameleon wrote:
>I am confused on this:
>When "new" fails, what happens?
>- throws bad_alloc
>- returns 0


Most probably neither nor. operator new allocates memory and calls a
constructor. If construction fails a constructor specific exception is
(better, may be) thrown. Out of memory is usually handled by a
new_handler that just terminates the application (you cannot 'handle'
out of memory). The std::bad_alloc exception is a textbook artefact
that is never seen in the real world.

Best wishes,
Roland Pibinger

red floyd 01-05-2007 11:06 PM

Re: new throws or returns?
 
Roland Pibinger wrote:
> On Fri, 05 Jan 2007 15:46:42 +0200, Chameleon wrote:
>> I am confused on this:
>> When "new" fails, what happens?
>> - throws bad_alloc
>> - returns 0

>
> Most probably neither nor. operator new allocates memory and calls a
> constructor. If construction fails a constructor specific exception is
> (better, may be) thrown. Out of memory is usually handled by a
> new_handler that just terminates the application (you cannot 'handle'
> out of memory). The std::bad_alloc exception is a textbook artefact
> that is never seen in the real world.


No, bad_alloc is mandated by the Standard. Just because *you* aren't
using standard-compliant compiler, don't assume the same for the rest of us.

AnonMail2005@gmail.com 01-06-2007 04:56 PM

Re: new throws or returns?
 


On Jan 5, 6:06 pm, red floyd <no.s...@here.dude> wrote:
> Roland Pibinger wrote:
> > On Fri, 05 Jan 2007 15:46:42 +0200, Chameleon wrote:
> >> I am confused on this:
> >> When "new" fails, what happens?
> >> - throws bad_alloc
> >> - returns 0

>
> > Most probably neither nor. operator new allocates memory and calls a
> > constructor. If construction fails a constructor specific exception is
> > (better, may be) thrown. Out of memory is usually handled by a
> > new_handler that just terminates the application (you cannot 'handle'
> > out of memory). The std::bad_alloc exception is a textbook artefact
> > that is never seen in the real world.No, bad_alloc is mandated by the Standard. Just because *you* aren't

> using standard-compliant compiler, don't assume the same for the rest of us.

That was the impression I got from reading the online Dinkumware C++
documentation.

To the orginal poster, the dinkumware site gives a very good
description of this.
Just search on "new handler". Also, Meyer's Effective C++, 3rd
Edition, goes
over this in great detail.



All times are GMT. The time now is 02:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.