473,549 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1824

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.infor matik.tu-darmstadt.dewro te in message
news:45******** *************** @newsspool4.arc or-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.infor matik.tu-darmstadt.dewro te in message
news:45******** *************** @newsspool4.arc or-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
30074
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 part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! ...
22
808
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
3200
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 never happened. Am i wrong somewhere or is it my VC++6 compiler giving me problems???? Thanks and Best Regards, Senthilvel.
2
1585
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 following appears: "Consider the simple function f() that appears to have nothing to do with exception handling: void g(int);
6
1662
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 result, is it okay to throw an exception? If I do, will the fstreams destructor get called? Will the file get closed? Something like: void...
0
960
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 how execution leaves the corresponding try block. Consider the following abbreviated code: // some unmanaged class __nogc class Foo {
10
1780
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 different functions are not. below is my code //program start void fun1() {int i;
5
1911
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 studied Computer Science so I like to try and 'catch up' on stuff like this at the weekend. Having said that I have a couple of questions around the...
3
6736
by: dami | last post by:
what is use of stack in exception handling.. and what is stack unwinding
0
7524
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7960
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7475
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.