views:

152

answers:

3

When a caller is higher in the stack, what does this mean? For example, lets say I start a program, a form loads up (we'll call this a), then this form calls another form (b). The called form will be at the top of the stack, so if this form called form a, will this be a caller higher in the stack making a call to something below?

Thanks

+1  A: 

You need to distinguish between the object making the call (if any), the target of the call, and the method being called. For instance, your call stack could easily look like this:

FormA.Method3()
FormB.Method2()
FormA.Method1()

This is an instance of FormA executing Method1, calling Method2 on an instance of FormB. That then calls Method3 on an instance of FormA - either the same FormA as the first one, or a different one. It doesn't really matter.

It's not really a case of calling "something below" because the stack frames don't represent objects - they represent methods (and the state within those methods). Does that help at all, or is it just confusing things more?

Jon Skeet
+5  A: 

I think you have the wrong impression of the call stack. The call stach is just a "list" of the functions that have been called. When ou have a call chain like you describe, a calls b which calls a, your stack is just:

a.second
b.first
a.first

You can't really call "down" to something. You make another call, and it goes on top of the stack, even if it has been called before, the previous call is completely different, the new call starts a whole new "stack frame".

SoapBox
A: 

That makes sense.

It's really just the order in which methods are called. Interestingly enough, I have used stackframes in the trace to get the last called method, etc.

My question came about when I read in a study guide for exam 70-536 (For those here that do C# and the exams), when there was a lesson on security and something about methods higher in the call stack and what they can call given the placement of permissions. I will try to find this segment in the book.

Thanks!

dotnetdev