Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > new throws or returns?

Reply
Thread Tools

new throws or returns?

 
 
Chameleon
Guest
Posts: n/a
 
      01-05-2007
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!
 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      01-05-2007

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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      01-05-2007
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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-05-2007
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.
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      01-05-2007
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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-05-2007
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.
 
Reply With Quote
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      01-06-2007


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What if new throws exeption? doublemaster007@gmail.com C++ 10 12-18-2009 09:20 AM
DataInputStream(new GZIPInputStream).readFully throws EOFException A. Farber Java 3 03-21-2006 11:20 AM
Visual Studio throws UnauthorizedAccessException when debugging R Warford ASP .Net 3 12-01-2003 04:08 PM
Web Service throws 401 Error when consumed Sanjay ASP .Net 4 11-20-2003 04:03 AM
XmlValidatingReader throws exception for SAOP-ENV:encodingStyle attribute Himmat Dhange ASP .Net 0 08-26-2003 08:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57