array exception safety

K

Klaus Ahrens

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...
}
//-------------------------------------------------------------------
 
T

tom_usenet

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
 

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

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top