472,354 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 2936
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,...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.