new & exception handling

Discussion in 'C++' started by Guest, Nov 30, 2003.

  1. Guest

    Guest Guest

    I have this code:
    ---------------------
    try {
    int *a = new int[1000000000];
    } catch (...)
    {
    cout << "oh no!"; exit(0);
    }
    ----------------------
    new returns 0 but no exception occur in this error. Why?
    ============
    I learn today about auto_ptr. What is the use of this? (I have not
    understand it well)
    if I want automatic deletion of int array inside a class constructor on
    exception I must use it like this?
    -------------------------
    auto_ptr<int> array_of_int(new int(100));
    -------------------------

    thanks
     
    Guest, Nov 30, 2003
    #1
    1. Advertising

  2. Guest

    Deming He Guest

    <- Chameleon -> <> wrote in message
    news:bqdau9$lcq$...
    > I have this code:
    > ---------------------
    > try {
    > int *a = new int[1000000000];
    > } catch (...)
    > {
    > cout << "oh no!"; exit(0);
    > }
    > ----------------------
    > new returns 0 but no exception occur in this error. Why?



    I double-checked B. Stroustrup's book on this: the defaulted behavior of
    exhausting store should be throwing a bad_alloc exception. As to not
    catching the 'bad_alloc' in your case it could be an evidence of deviation
    of the compiler from standards.


    > ============
    > I learn today about auto_ptr. What is the use of this? (I have not
    > understand it well)


    The object pointed by an auto_ptr will be implicitly deleted at the end of
    the scope of the said auto_ptr. Thus, you don't have to call delete in case
    you forget - this save you from possible memory leak.


    > if I want automatic deletion of int array inside a class constructor on
    > exception I must use it like this?
    > -------------------------
    > auto_ptr<int> array_of_int(new int(100));
    > -------------------------
    >

    I don't understand what you mean here...Also, is that a typo (new int(100))
    if you wanted an array of int?

    Generally you cannot use auto_ptr with an array; use other template classes
    instead.
     
    Deming He, Nov 30, 2003
    #2
    1. Advertising

  3. <- Chameleon -> wrote:
    > I have this code:
    > ---------------------
    > try {
    > int *a = new int[1000000000];
    > } catch (...)
    > {
    > cout << "oh no!"; exit(0);
    > }
    > ----------------------
    > new returns 0 but no exception occur in this error. Why?
    > ============
    > I learn today about auto_ptr. What is the use of this? (I have not
    > understand it well)
    > if I want automatic deletion of int array inside a class constructor on
    > exception I must use it like this?
    > -------------------------
    > auto_ptr<int> array_of_int(new int(100));
    > -------------------------
    >
    > thanks
    >
    >

    The compiler I use (cxx) throws an exception only if the source code is
    compiled with an option to the compiler telling it to use std new.
    Otherwise, 0 is returned instead of throwing an exception. You probably
    need to tell your compiler to use std new.

    / Peter
     
    Peter Johansson, Dec 1, 2003
    #3
  4. Guest

    Guest Guest

    > > I have this code:
    > > ---------------------
    > > try {
    > > int *a = new int[1000000000];
    > > } catch (...)
    > > {
    > > cout << "oh no!"; exit(0);
    > > }
    > > ----------------------
    > > new returns 0 but no exception occur in this error. Why?
    > > ============
    > > I learn today about auto_ptr. What is the use of this? (I have not
    > > understand it well)
    > > if I want automatic deletion of int array inside a class constructor on
    > > exception I must use it like this?
    > > -------------------------
    > > auto_ptr<int> array_of_int(new int(100));
    > > -------------------------
    > >
    > > thanks
    > >
    > >

    > The compiler I use (cxx) throws an exception only if the source code is
    > compiled with an option to the compiler telling it to use std new.
    > Otherwise, 0 is returned instead of throwing an exception. You probably
    > need to tell your compiler to use std new.


    above of this code I use
    using namespace std;
     
    Guest, Dec 1, 2003
    #4
  5. Guest

    red floyd Guest

    <- Chameleon -> wrote:
    >>>I have this code:
    >>>---------------------
    >>>try {
    >>>int *a = new int[1000000000];
    >>>} catch (...)
    >>>{
    >>>cout << "oh no!"; exit(0);
    >>>}
    >>>----------------------
    >>>new returns 0 but no exception occur in this error. Why?
    >>>============
    >>>I learn today about auto_ptr. What is the use of this? (I have not
    >>>understand it well)
    >>>if I want automatic deletion of int array inside a class constructor on
    >>>exception I must use it like this?
    >>>-------------------------
    >>>auto_ptr<int> array_of_int(new int(100));
    >>>-------------------------
    >>>
    >>>thanks
    >>>
    >>>

    >>
    >>The compiler I use (cxx) throws an exception only if the source code is
    >>compiled with an option to the compiler telling it to use std new.
    >>Otherwise, 0 is returned instead of throwing an exception. You probably
    >>need to tell your compiler to use std new.

    >
    >
    > above of this code I use
    > using namespace std;
    >
    >


    No. auto_ptr is not for arrays. The destructor calls delete, not delete[].

    As for the lack of exception on new , what compiler are you using? You also could look specifically for a std::bad_alloc exception.
     
    red floyd, Dec 1, 2003
    #5
  6. Guest

    Guest Guest

    > >>>I have this code:
    > >>>---------------------
    > >>>try {
    > >>>int *a = new int[1000000000];
    > >>>} catch (...)
    > >>>{
    > >>>cout << "oh no!"; exit(0);
    > >>>}
    > >>>----------------------
    > >>>new returns 0 but no exception occur in this error. Why?
    > >>>============
    > >>>I learn today about auto_ptr. What is the use of this? (I have not
    > >>>understand it well)
    > >>>if I want automatic deletion of int array inside a class constructor on
    > >>>exception I must use it like this?
    > >>>-------------------------
    > >>>auto_ptr<int> array_of_int(new int(100));
    > >>>-------------------------
    > >>
    > >>The compiler I use (cxx) throws an exception only if the source code is
    > >>compiled with an option to the compiler telling it to use std new.
    > >>Otherwise, 0 is returned instead of throwing an exception. You probably
    > >>need to tell your compiler to use std new.

    > >
    > >
    > > above of this code I use
    > > using namespace std;
    > >
    > >

    >
    > No. auto_ptr is not for arrays. The destructor calls delete, not

    delete[].
    >
    > As for the lack of exception on new , what compiler are you using? You

    also could look specifically for a std::bad_alloc exception.

    I use MS VC++ 6
     
    Guest, Dec 2, 2003
    #6
  7. Guest

    Mike Wahler Guest

    "<- Chameleon ->" <> wrote in message
    news:bqicd9$50a$...
    > > >>>I have this code:
    > > >>>---------------------
    > > >>>try {
    > > >>>int *a = new int[1000000000];
    > > >>>} catch (...)
    > > >>>{
    > > >>>cout << "oh no!"; exit(0);
    > > >>>}
    > > >>>----------------------
    > > >>>new returns 0 but no exception occur in this error. Why?
    > > >>>============
    > > >>>I learn today about auto_ptr. What is the use of this? (I have not
    > > >>>understand it well)
    > > >>>if I want automatic deletion of int array inside a class constructor

    on
    > > >>>exception I must use it like this?
    > > >>>-------------------------
    > > >>>auto_ptr<int> array_of_int(new int(100));
    > > >>>-------------------------
    > > >>
    > > >>The compiler I use (cxx) throws an exception only if the source code

    is
    > > >>compiled with an option to the compiler telling it to use std new.
    > > >>Otherwise, 0 is returned instead of throwing an exception. You

    probably
    > > >>need to tell your compiler to use std new.
    > > >
    > > >
    > > > above of this code I use
    > > > using namespace std;
    > > >
    > > >

    > >
    > > No. auto_ptr is not for arrays. The destructor calls delete, not

    > delete[].
    > >
    > > As for the lack of exception on new , what compiler are you using? You

    > also could look specifically for a std::bad_alloc exception.
    >
    > I use MS VC++ 6


    VC++6 default behavior upon failure of operator new is
    nonstandard (but there is a way to work around that).
    See: http://tinyurl.com/xef9

    -Mike
     
    Mike Wahler, Dec 2, 2003
    #7
  8. Guest

    Gavin Deane Guest

    "<- Chameleon ->" <> wrote in message news:<bqicd9$50a$>...
    > > >>>I have this code:
    > > >>>---------------------
    > > >>>try {
    > > >>>int *a = new int[1000000000];
    > > >>>} catch (...)
    > > >>>{
    > > >>>cout << "oh no!"; exit(0);
    > > >>>}
    > > >>>----------------------
    > > >>>new returns 0 but no exception occur in this error. Why?
    > > >>>============
    > > >>>I learn today about auto_ptr. What is the use of this? (I have not
    > > >>>understand it well)
    > > >>>if I want automatic deletion of int array inside a class constructor on
    > > >>>exception I must use it like this?
    > > >>>-------------------------
    > > >>>auto_ptr<int> array_of_int(new int(100));
    > > >>>-------------------------
    > > >>
    > > >>The compiler I use (cxx) throws an exception only if the source code is
    > > >>compiled with an option to the compiler telling it to use std new.
    > > >>Otherwise, 0 is returned instead of throwing an exception. You probably
    > > >>need to tell your compiler to use std new.
    > > >
    > > >
    > > > above of this code I use
    > > > using namespace std;
    > > >
    > > >

    > >
    > > No. auto_ptr is not for arrays. The destructor calls delete, not

    > delete[].
    > >
    > > As for the lack of exception on new , what compiler are you using? You

    > also could look specifically for a std::bad_alloc exception.
    >
    > I use MS VC++ 6


    <OT>
    MSVC++6 doesn't throw std::bad_alloc when new fails, it just returns
    null. This is non-standard behaviour and I don't think there's an easy
    work-around
    </OT>

    As others have said, you can't use auto_ptr to hold an array. But you
    might find something useful in the smart pointer library at
    http://www.boost.org.

    --
    hth
    GJD
     
    Gavin Deane, Dec 3, 2003
    #8
    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. Kerri
    Replies:
    2
    Views:
    13,115
    Kevin Spencer
    Oct 27, 2003
  2. jayapal
    Replies:
    1
    Views:
    392
    James Kanze
    Jul 18, 2007
  3. jayapal
    Replies:
    3
    Views:
    403
  4. Peter
    Replies:
    34
    Views:
    2,018
    James Kanze
    Oct 17, 2009
  5. VSK
    Replies:
    0
    Views:
    267
Loading...

Share This Page