473,549 Members | 3,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stack questions

DC
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 excution stack, in
particular 2 things are bothing me:

1. It is said that on encountering an exception, the execution engine
"unwinds" the call stack looking for a suitable exception handler. In the
example below can we assume that the address of the error handling code is
pushed onto the stack as the return address before executing the try block
and that, infact, the code within the try block is in itself executed as a
function call within it's own stack frame? This can't be the answer though
as the catch block only executes where the stack is unwinding 'in error' not
simply in unwinding during normal flow. So what is really going on??

try
{
//some work
}
catch
{
//handling code
}

2. Continuations in C# - are they really unwinding and rewinding the stack -
or is there some sort of complirer magic going on that creates an invisible
state machine?
Love to know your thoughts on the above.
Cheers
Oct 22 '06 #1
5 1910
I don't know exactly how Microsoft implements the throw/catch logic, but a
classical way to do it is to save the value of the registers just before the
try block and to implement throw by restoring the registers.

In C, this can be done with setjmp and longjmp. See
http://www.di.unipi.it/~nids/docs/lo...ow_catch.shtml for details.

Bruno

Of course, you need some logic to chain the areas where you save the
registers, so that the throw can find the last

"DC" <***@***.coma écrit dans le message de news:
e8************* *@TK2MSFTNGP05. phx.gbl...
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 excution stack,
in particular 2 things are bothing me:

1. It is said that on encountering an exception, the execution engine
"unwinds" the call stack looking for a suitable exception handler. In the
example below can we assume that the address of the error handling code is
pushed onto the stack as the return address before executing the try block
and that, infact, the code within the try block is in itself executed as a
function call within it's own stack frame? This can't be the answer though
as the catch block only executes where the stack is unwinding 'in error'
not simply in unwinding during normal flow. So what is really going on??

try
{
//some work
}
catch
{
//handling code
}

2. Continuations in C# - are they really unwinding and rewinding the
stack - or is there some sort of complirer magic going on that creates an
invisible state machine?
Love to know your thoughts on the above.
Cheers

Oct 22 '06 #2
DC <***@***.comwro te:
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 excution stack, in
particular 2 things are bothing me:

1. It is said that on encountering an exception, the execution engine
"unwinds" the call stack looking for a suitable exception handler. In the
example below can we assume that the address of the error handling code is
pushed onto the stack as the return address before executing the try block
and that, infact, the code within the try block is in itself executed as a
function call within it's own stack frame? This can't be the answer though
as the catch block only executes where the stack is unwinding 'in error' not
simply in unwinding during normal flow. So what is really going on??
I believe that when the exception is thrown, the JIT knows about
exception handlers within each stack frame - it doesn't necessarily
need to unwind the stack, as you've pointed out. The basic principle is
that execution jumps to the innermost exception handler which is
capable of handling that exception type.
2. Continuations in C# - are they really unwinding and rewinding the stack -
or is there some sort of complirer magic going on that creates an invisible
state machine?
I assume you mean the "yield" statement? It's creating a state machine.
If you decompile the generated code, it can be very interesting to look
through.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '06 #3
DC
My thanks to both who replied.

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
DC <***@***.comwro te:
>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 excution stack,
in
particular 2 things are bothing me:

1. It is said that on encountering an exception, the execution engine
"unwinds" the call stack looking for a suitable exception handler. In the
example below can we assume that the address of the error handling code
is
pushed onto the stack as the return address before executing the try
block
and that, infact, the code within the try block is in itself executed as
a
function call within it's own stack frame? This can't be the answer
though
as the catch block only executes where the stack is unwinding 'in error'
not
simply in unwinding during normal flow. So what is really going on??

I believe that when the exception is thrown, the JIT knows about
exception handlers within each stack frame - it doesn't necessarily
need to unwind the stack, as you've pointed out. The basic principle is
that execution jumps to the innermost exception handler which is
capable of handling that exception type.
>2. Continuations in C# - are they really unwinding and rewinding the
stack -
or is there some sort of complirer magic going on that creates an
invisible
state machine?

I assume you mean the "yield" statement? It's creating a state machine.
If you decompile the generated code, it can be very interesting to look
through.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 23 '06 #4
DC

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
DC <***@***.comwro te:
>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 excution stack,
in
particular 2 things are bothing me:

1. It is said that on encountering an exception, the execution engine
"unwinds" the call stack looking for a suitable exception handler. In the
example below can we assume that the address of the error handling code
is
pushed onto the stack as the return address before executing the try
block
and that, infact, the code within the try block is in itself executed as
a
function call within it's own stack frame? This can't be the answer
though
as the catch block only executes where the stack is unwinding 'in error'
not
simply in unwinding during normal flow. So what is really going on??

I believe that when the exception is thrown, the JIT knows about
exception handlers within each stack frame - it doesn't necessarily
need to unwind the stack, as you've pointed out. The basic principle is
that execution jumps to the innermost exception handler which is
capable of handling that exception type.
>2. Continuations in C# - are they really unwinding and rewinding the
stack -
or is there some sort of complirer magic going on that creates an
invisible
state machine?

I assume you mean the "yield" statement? It's creating a state machine.
If you decompile the generated code, it can be very interesting to look
through.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 23 '06 #5

"DC" <***@***.comwro te in message
news:e8******** ******@TK2MSFTN GP05.phx.gbl...
| 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 excution stack,
in
| particular 2 things are bothing me:
|
| 1. It is said that on encountering an exception, the execution engine
| "unwinds" the call stack looking for a suitable exception handler. In the
| example below can we assume that the address of the error handling code is
| pushed onto the stack as the return address before executing the try block
| and that, infact, the code within the try block is in itself executed as a
| function call within it's own stack frame? This can't be the answer though
| as the catch block only executes where the stack is unwinding 'in error'
not
| simply in unwinding during normal flow. So what is really going on??
|
| try
| {
| //some work
| }
| catch
| {
| //handling code
| }
|
A two phase exception handling model is employed by the CLR, and CLR
exceptions are implemented on top of the Windows SEH mechanism (as are the
C++ exceptions).
When an exception is thrown, the CLR initiates the first phase which consist
of a stack walk to search for an appropriate exception handler (the one
who's filter is able to deal with the exception!), in the second phase the
stack is unwound until the frame containing the handler is reached. Note
that unwinding the stack, causes the finally blocks on the unwounded frames
to be executed.
More gory detail, can be found here:
http://blogs.msdn.com/cbrumme/archiv.../01/51524.aspx

Willy.
Oct 23 '06 #6

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

Similar topics

15
7715
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is how this would work with class objects. In this case do you have to push the object on the stack at the start of every public procedure etc. in the...
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! ...
0
1261
by: CoderGuy | last post by:
Hello I am reading up a bit on how memmory is used in the .NET Framework and have a few question about the stack-based approach * I understand that the stack is used to provide a layer of abstractio between the CLR and the underlaying memory hardware (porting to anothe architecture should not change the program, i.e you dont work with...
2
2475
by: news.tkdsoftware.com | last post by:
Aside from comp.compilers, is there any other forum, newsgroup or medium where I can post questions concerning the development of a byte code compiler & virtual stack machine? --
4
3606
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
8
2077
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to ignore spaces, case sensitivity and punctuation. I need three text boxes. The first will be for input of the string. The second will display the string...
4
2138
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class stack { public:
11
2898
by: Nehil | last post by:
I would like to know which is dynamic in nature. if i refer the C memory model (Richard Steven), it is shown that both stack and heap grow towards each other. Now, can one go into other's area and hence effecting the size of other memory area. Does any limit exist upto which a stack or a heap can grow. and if it is there then who decides...
87
5485
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first place? I can see two definite disadvantages with this: 1) deeply nested recursive calls to a function (especially if it defines
0
7520
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
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7718
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
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...
0
7809
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...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
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.