473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Stack unwinding and destructors?

On page 366 of TC++PL(SE) we are told " The destructor will be called
independently of whether the function is exited normally or exited because
an exception is thrown."

On page 382 the following appears:

"Consider the simple function f() that appears to have nothing to do with
exception handling:

void g(int);

void f()
{
string s;
//...
g(1);
g(2);
}

However, g() may throw an exception, so f() must contain code ensuring that
s is destroyed correctly in case of an exception."

These statements seem mutually inconsistent. Would it have been more
correct to use 'string* s = new string();' in place of "string s;"?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #1
2 1578
On Mon, 09 Aug 2004 14:07:46 -0400, Steven T. Hatton
<su******@setidava.kushan.aa> wrote:
On page 366 of TC++PL(SE) we are told " The destructor will be called
independently of whether the function is exited normally or exited
because
an exception is thrown."

On page 382 the following appears:

"Consider the simple function f() that appears to have nothing to do with
exception handling:

void g(int);

void f()
{
string s;
//...
g(1);
g(2);
}

However, g() may throw an exception, so f() must contain code ensuring
that
s is destroyed correctly in case of an exception."
I think 'must contain code' could be better expressed as 'the compiler
must place code'. There is nothing that the programmer need do to ensure
that the destructor for s is called, either by stack unwinding or normal
function exit.

These statements seem mutually inconsistent. Would it have been more
correct to use 'string* s = new string();' in place of "string s;"?


I don't think so, that would be a guaranteed memory leak, however the
function was exitted.

john
Jul 22 '05 #2
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in
news:0O********************@speakeasy.net:
On page 366 of TC++PL(SE) we are told " The destructor will be called
independently of whether the function is exited normally or exited
because an exception is thrown."
Yep
On page 382 the following appears:

"Consider the simple function f() that appears to have nothing to do
with exception handling:

void g(int);

void f()
{
string s;
//...
g(1);
g(2);
}

However, g() may throw an exception, so f() must contain code ensuring
that s is destroyed correctly in case of an exception."

These statements seem mutually inconsistent. Would it have been more
correct to use 'string* s = new string();' in place of "string s;"?


More correct in what way? And how are the statements mutually
inconsistent?
Jul 22 '05 #3

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

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
22
by: MSG | last post by:
Hello void f1(int n) { vector<int> x(n); /* C++ */ } void f2(int n) { int x; /* C99 only */ } void f3(int n) { int* x = new int; /* C++ */ delete x; }
6
by: John Ratliff | last post by:
What happens to objects created on the stack when you throw an exception? For example, suppose I opened a file for reading with an fstream. If I perform a read operation and I don't like the...
0
by: i.r.f. | last post by:
Recently I ran into a problem that I traced to something which completely surprised me: the relative order of stack unwinding and finally block execution in mixed C++ appears to vary depending on...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
5
by: DC | last post by:
Hi all, First of all, let me asure you you will not be doing my homework for me if you are good enough to reply - I have been a professional programmer for a couple of years now, I just never...
4
by: Tony | last post by:
Well the subject says it all. Do I have to have it if I use classes in C++? Tony
3
by: dami | last post by:
what is use of stack in exception handling.. and what is stack unwinding
2
by: George2 | last post by:
Hello everyone, I listed for scenarios. I think case 1, 2 and 4 are object's destructor is called during stack unwinding caused by exception. Scenario 3 is not stack unwinding and it is normal...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.