473,473 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

array exception safety

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[i]){}
// Xs(int i) try :n(i), data(new X[i]){}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[i]){}
Xs(int i) try :n(i), data(new X[i]){}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...
}
//-------------------------------------------------------------------
Jul 22 '05 #1
1 2112
On Mon, 08 Dec 2003 13:20:47 +0100, Klaus Ahrens
<ah****@informatik.hu-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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Harrison | last post by:
Where can I find information on exception safety in the STL? I.e. which methods on which types offer what level of exception safety. Josuttis has a useful list of classes and methods but he fails...
7
by: Arthur Sinko | last post by:
Hi, Is it possible to answer the following question: what is the difference between two declarations: vector<double> a(n); and double a;
3
by: Jesika | last post by:
Hi, I have a cumbersome question: Given the following declaration: string varray = new string; (15 blocks of memory) Does it allocate a contiguous blocks of memory or not? If it does, is...
4
by: robinsand | last post by:
Header File: car.h #if !defined CAR_H #define CAR_H enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize, ctMiniVan, ctSUV }; class Car { public: Car();
26
by: Adam Warner | last post by:
Hi all, One cannot return a pointer to an array type in C because C has no first class array types. But one can return a pointer to a struct containing an incomplete array via the illegal but...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
14
by: jehugaleahsa | last post by:
Hello: As an avid reader of C++ books, I know a lot of programmers out there are confronted with the challenge of exception safety. For those who don't know what it is, it is writing code in...
1
by: Vincent Jacques | last post by:
Hello all, I'm writing a class with value semantic, and I want its public interface to provide the strong exception safety guaranty (if a public operation fails for any reason, then an...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.