array exception safety

Discussion in 'C++' started by Klaus Ahrens, Dec 8, 2003.

  1. Klaus Ahrens

    Klaus Ahrens Guest

    if arrays were exception save (as i thought) the following should give
    2 destructors for the partly constructed x2. unfortunately none of the
    compilers i tried (g++3.3.1, vc++.net, bcc32, icc) conforms to this.

    when using the (commented) Xs-ctor with try-rethrow at least g++ and
    vc++.net show really two destructors.

    even worse, when taking the vector-based Xsv instead if Xs in main
    there is only ONE dtor-call and two with the try-rethrowing Xsv-ctor.

    very puzzled:
    k ahrens

    //-------------------------------------------------------------------
    #include <iostream>
    #include <vector>

    using std::cerr;
    using std::endl;
    using std::vector;

    class X {
    static int c;
    public:
    X(){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
    X(const X&){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
    ~X() { ++c; cerr<<"~X()"<<endl;;}
    };

    int X::c=2;

    class Xs {
    int n;
    X* data;
    public:
    Xs(int i):n(i), data(new X){}
    // Xs(int i) try :n(i), data(new X){}catch(...){throw;}
    ~Xs(){ delete[] data; }
    };

    class Xsv {
    int n;
    vector<X> *data;
    public:
    Xsv(int i):n(i), data(new vector<X>(i)){}
    // Xsv(int i) try :n(i), data(new vector<X>(i)){}
    // catch(...){throw;}
    ~Xsv(){ delete data; }
    };

    int main()
    {
    {Xs x2(4);} // resp. Xsv...
    }
    //-------------------------------------------------------------------




    //-------------------------------------------------------------------
    #include <iostream>
    #include <vector>

    using std::cerr;
    using std::endl;
    using std::vector;

    class X {
    static int c;
    public:
    X(){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
    X(const X&){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
    ~X() { ++c; cerr<<"~X()"<<endl;;}
    };

    int X::c=2;

    class Xs {
    int n;
    X* data;
    public:
    // Xs(int i):n(i), data(new X){}
    Xs(int i) try :n(i), data(new X){}catch(...){throw;}
    ~Xs(){ delete[] data; }
    };

    class Xsv {
    int n;
    vector<X> *data;
    public:
    // Xsv(int i):n(i), data(new vector<X>(i)){}
    Xsv(int i) try :n(i), data(new vector<X>(i)){}
    catch(...){throw;}
    ~Xsv(){ delete data; }
    };

    int main()
    {
    {Xsv x2(4);} // resp. Xsv...
    }
    //-------------------------------------------------------------------
     
    Klaus Ahrens, Dec 8, 2003
    #1
    1. Advertising

  2. Klaus Ahrens

    tom_usenet Guest

    On Mon, 08 Dec 2003 13:20:47 +0100, Klaus Ahrens
    <-berlin.de> wrote:

    >if arrays were exception save (as i thought) the following should give
    >2 destructors for the partly constructed x2. unfortunately none of the
    >compilers i tried (g++3.3.1, vc++.net, bcc32, icc) conforms to this.


    Arrays are fine for exception safety. AFAIK, those compilers have no
    trouble with destroying partly constructed arrays.

    >
    >when using the (commented) Xs-ctor with try-rethrow at least g++ and
    >vc++.net show really two destructors.
    >
    >even worse, when taking the vector-based Xsv instead if Xs in main
    >there is only ONE dtor-call and two with the try-rethrowing Xsv-ctor.
    >
    >very puzzled:


    If you don't catch an exception (you let it escape from main),
    std::terminate is called, and destructors may not be called (it is
    implementation defined whether the stack unwinds or not before the
    terminate call). You can fix you programs by putting a try/catch in
    main, thus forcing a stack unwind.

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Dec 8, 2003
    #2
    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. John Harrison

    Exception safety in the STL

    John Harrison, Jul 6, 2003, in forum: C++
    Replies:
    5
    Views:
    4,108
    John Harrison
    Jul 8, 2003
  2. Scott Brady Drummonds
    Replies:
    3
    Views:
    913
    Dietmar Kuehl
    Jan 18, 2005
  3. Luke Meyers

    STL exception safety, operator==

    Luke Meyers, Jan 5, 2006, in forum: C++
    Replies:
    0
    Views:
    372
    Luke Meyers
    Jan 5, 2006
  4. benben
    Replies:
    3
    Views:
    331
    Frank Puck
    Nov 27, 2006
  5. Aarti

    Vector exception safety

    Aarti, Jul 20, 2007, in forum: C++
    Replies:
    3
    Views:
    435
    James Kanze
    Jul 22, 2007
Loading...

Share This Page