473,404 Members | 2,170 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,404 software developers and data experts.

exception handling, function call and memory

Hello everyone,
Such code segment is used to check whether function call or exception-
handling mechanism runs out of memory first (written by Bjarne),

Expand|Select|Wrap|Line Numbers
  1. void perverted()
  2. {
  3. try{
  4. throw exception();
  5. }
  6. catch (exception& e)
  7. {
  8. perverted();
  9. cout << e.what() << endl;
  10. }
  11. }
  12.  
  13.  
1.

My question is when the exception is thrown, and goes to exception
handler in catch block, it will do operations in the following
sequences,

(1) execute the exception handler code (since there will be recursive
function call -- stack will ever increasing);
(2) unwind stack.

Runs out of memory because function call will make stack ever-
increasing, right?

If it does (2) before (1), I think stack will always be unwinded
before exception handling is called -- then stack will never grow, so
there will be no out-of-memory caused by an ever increasing stack.

Is that correct?

2.

I am confused about why exception handling mechanism will run out of
memory because in exception handle implementation, we just insert
exception handler registration block of the function onto the
beginning of the new function call stack each time there is a function
call, so the memory should be the memory consumed by function call
stuff, I do not think exception handling mechanism itself will consume
additional memory. How do you think of it and how do you think of the
what are the memory consumed by exception handling mechanism?
thanks in advance,
George
Jan 30 '08 #1
1 3076
On Jan 30, 11:32 am, "Alf P. Steinbach" <al...@start.nowrote:
* George2:
Such code segment is used to check whether function call or
exception- handling mechanism runs out of memory first
(written by Bjarne),
Expand|Select|Wrap|Line Numbers
  1.  void perverted()
  2.  {
  3.      try{
  4.          throw exception();
  5.      }
  6.      catch (exception& e)
  7.      {
  8.          perverted();
  9.          cout << e.what() << endl;
  10.      }
  11.  }
Expand|Select|Wrap|Line Numbers
  1.  
  2.         
  3.                         
  4.  
  5.  
  6.  
1.
My question is when the exception is thrown, and goes to exception
handler in catch block, it will do operations in the following
sequences,
(1) execute the exception handler code (since there will be recursive
function call -- stack will ever increasing);
(2) unwind stack.
From the C++ view, ignoring details of how exception handling
is implemented at the machine code level (e.g. double stack
walk), a 'throw' results in stack unwinding followed by
execution of 'catch' code, if there is a matching 'catch'.
Runs out of memory because function call will make stack ever-
increasing, right?
From an academic point of view that depends on optimization. A
compiler could conceivably detect that the above is an infinite
recursion doing nothing. In practice it will run out of memory.
If it does (2) before (1), I think stack will always be unwinded
before exception handling is called -- then stack will never grow, so
there will be no out-of-memory caused by an ever increasing stack.
Is that correct?
No. In the code above the "stack unwinding" is only locally
within the function call, essentially doing nothing, not back
to a call of the function.
I'm not sure that that was the point of the example, however.
(It's a lot easier to get infinite recursion:).) The point
here, I think, is that the exception cannot be destructed before
the catch clause which handles it has finished. And in the
meantime, another exception is raised. The compiler cannot use
static memory for the exception itself---it must use some sort
of stack-like mechanism. And code like the above will cause
this "exception stack" to overflow. (On the machines I usually
work on, this exception stack will overflow long before you'd
get normal stack overflow.)
2.
I am confused about why exception handling mechanism will
run out of memory because in exception handle
implementation, we just insert exception handler
registration block of the function onto the beginning of the
new function call stack each time there is a function call,
so the memory should be the memory consumed by function call
stuff, I do not think exception handling mechanism itself
will consume additional memory. How do you think of it and
how do you think of the what are the memory consumed by
exception handling mechanism?
Whether a 'try' consumes memory depends on the implementation.
Whether the try consumes memory or not, the thrown exceptions
certainly will.
PS: George, could you please start giving references for your
quotes and parahprases, and also, please start reading the
groups you post to.
Will all people not present please step forward:-). (I know.
It's frustrating me, too.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 31 '08 #2

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

Similar topics

3
by: John Ruiz | last post by:
Hello. I was wondering about exceptions and how to throw them within functions/methods and catch them. Suppose that we've got: ----------------------------------------------------------------...
5
by: Bikash | last post by:
Hello, I am a specific problem in exception handling. The code snippets is attached below. void f() { char *ptr = new char(20); throw 2; }
11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
3
by: Alberto Giménez | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi there! I'm doing some homework (almost finished), but now i'm reconsidering a design decission I made at the beginning. This is about error...
6
by: vkreddy_in | last post by:
HI Currently i am handling excpetion using TRY/catch in my code.I am looking for solution some thing like this. if an excrption is occured then 1.call a CALLBACK function in a DLL 2.print the...
19
by: KKramsch | last post by:
One of the features from other languages that I miss most in C is trappable exceptions. More specifically, I think it's great to be able to demarcate a whole block of code where several exceptions...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
9
by: jg | last post by:
Can someone explain what the standard says about exception context ? For example, suppose bar() always throw an exception, is the value of g undefined or 1 after the handler (empty in this case)...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
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: 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,...
0
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.