new throws or returns?

Discussion in 'C++' started by Chameleon, Jan 5, 2007.

  1. Chameleon

    Chameleon Guest

    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!
     
    Chameleon, Jan 5, 2007
    #1
    1. Advertising

  2. Chameleon

    Ondra Holub Guest

    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
     
    Ondra Holub, Jan 5, 2007
    #2
    1. Advertising

  3. Chameleon

    mlimber Guest

    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
     
    mlimber, Jan 5, 2007
    #3
  4. Chameleon

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Jan 5, 2007
    #4
  5. 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
     
    Roland Pibinger, Jan 5, 2007
    #5
  6. Chameleon

    red floyd Guest

    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.
     
    red floyd, Jan 5, 2007
    #6
  7. Chameleon

    Guest

    On Jan 5, 6:06 pm, red floyd <> 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.
     
    , Jan 6, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vizlab
    Replies:
    3
    Views:
    4,277
    Michael Bar-Sinai
    Oct 17, 2007
  2. A. Farber
    Replies:
    3
    Views:
    6,614
    A. Farber
    Mar 21, 2006
  3. ++imanshu
    Replies:
    7
    Views:
    500
    ++imanshu
    Aug 23, 2008
  4. Trans
    Replies:
    2
    Views:
    252
    Trans
    Nov 6, 2005
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    676
    David A. Black
    Jul 2, 2008
Loading...

Share This Page