new throws or returns?

C

Chameleon

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

Ondra Holub

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
 
M

mlimber

Ondra said:
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
 
R

Rolf Magnus

Chameleon said:
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.
 
R

Roland Pibinger

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
 
R

red floyd

Roland said:
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.
 
A

AnonMail2005

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top