![]() |
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! |
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 |
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 |
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. |
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 |
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. |
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 09:45 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.