473,385 Members | 1,973 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: can I avoid it?

Well the subject says it all. Do I have to have it if I use classes in C++?

Tony
Dec 22 '06 #1
4 1812

Tony wrote:
Well the subject says it all. Do I have to have it if I use classes in C++?
A C++ program can use setjmp/longjmp to transfer execution to an
enclosing scope without "unwinding" the stack (that is, invoking the
destructors of objects with lifetimes bound to an intervening scope).
But I cannot think of a good reason why a C++ program would resort to
this technique, so I suspect there is a better question that could
asked instead of this one.

Greg

Dec 22 '06 #2
Tony wrote:
Well the subject says it all. Do I have to have it if I use classes in
C++?
1. Don't post your questions in the subject.
2. "The process of calling destructors for automatic objects constructed on
the path from a try block to a throw-expression is called 'stack
unwinding.'"

I don't see how 2 and "do I have to have it if I use classes in C++" fit
together.

Markus

Dec 22 '06 #3

"Markus Moll" <mo**@rbg.informatik.tu-darmstadt.dewrote in message
news:45***********************@newsspool4.arcor-online.net...
Tony wrote:
>Well the subject says it all. Do I have to have it if I use classes in
C++?

1. Don't post your questions in the subject.
2. "The process of calling destructors for automatic objects constructed
on
the path from a try block to a throw-expression is called 'stack
unwinding.'"

I don't see how 2 and "do I have to have it if I use classes in C++" fit
together.
Well I was wondering what the alternative there is, if any, in the face of
exception. Sure, if I say that all exceptions will be handled by terminating
the program, then I can rely on the OS to clean up and/or recover things
like memory and open files (in which case I don't need C++ exceptions at
all). But if I want to try and recover from an exceptional condition and
keep running the program, then the stack needs to be unwound.

I guess the real question is can I avoid using exceptions. I think that is
only possible if I terminate the program upon exceptional conditions. (?)

Tony
Dec 23 '06 #4

Tony wrote:
"Markus Moll" <mo**@rbg.informatik.tu-darmstadt.dewrote in message
news:45***********************@newsspool4.arcor-online.net...
Tony wrote:
Well the subject says it all. Do I have to have it if I use classes in
C++?
1. Don't post your questions in the subject.
2. "The process of calling destructors for automatic objects constructed
on
the path from a try block to a throw-expression is called 'stack
unwinding.'"

I don't see how 2 and "do I have to have it if I use classes in C++" fit
together.

Well I was wondering what the alternative there is, if any, in the face of
exception. Sure, if I say that all exceptions will be handled by terminating
the program, then I can rely on the OS to clean up and/or recover things
like memory and open files (in which case I don't need C++ exceptions at
all). But if I want to try and recover from an exceptional condition and
keep running the program, then the stack needs to be unwound.
The stack is unwound when an exception is thrown - which is exactly
what should happen, especially if the exception is caught and the
program resumes normal execution. And if the exception is not caught,
then the program will terminate. And unless the program has some
special requirements with regards to freeing resources - exiting
without unwinding the stack generally presents no problems.

Now the first question is whether your program will throw its own
exceptions or not. If the program does not throw exceptions than the
only exceptions that would be thrown would be from the library routines
it calls, such as the ones from the Standard Library. The Standard
Library routines may throw exceptions (for example, a memory allocation
failure when calling new()) and all of the exceptions that may be
thrown are documented.

Exceptions thrown from the Standard Library raises the second question
that has to be answered: whether your program will catch exceptions,
and if so, what kind of exceptions will it catch and where in the
program will it catch them, and what will the program do after having
caught a thrown exception. Generally, it is considered more
user-friendly for a program to catch all exceptions (even if the
program will terminate), in order to perform such actions as logging
the error, notifying the user of the problem, closing files, or any
other actions needed to make its exit as graceful as possible under the
circumstances.
I guess the real question is can I avoid using exceptions. I think that is
only possible if I terminate the program upon exceptional conditions. (?)
C++ exceptions were designed with the assumption that a C++ program
will catch most thrown exceptions and be likely resume its execution
afterwards. Otherwise, the program could simply call abort() or
terminate() as soon as the error was detected.

Greg

Dec 23 '06 #5

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; }
5
by: Senthilvel | last post by:
Hi, The automatic variables declared in the try bock will be destroyed when an exception is thrown due to stack unwinding. So i expected the following code to call the destructor of B but it...
2
by: Steven T. Hatton | last post by:
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...
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...
10
by: ypjofficial | last post by:
Hello All, since the programs' stack is shared among all the function inside the program, I was just doing some R&D to see whether the same stack space is used for the variables inside the...
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...
3
by: dami | last post by:
what is use of stack in exception handling.. and what is stack unwinding
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...

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.