Information Technology Reference
In-Depth Information
Stack Frames
So far, you know that local variables and parameters are kept on the stack. Let's look at that
organization a little further.
When a method is called, memory is allocated at the top of the stack, to hold a number of
data items associated with the method. This chunk of memory is called the stack frame for the
method.
￿
The stack frame contains memory to hold the following:
-
The return address—that is, where to resume execution when the method exits
-
Those parameters that allocate memory, that is, the value parameters of the method,
and the parameter array if there is one
-
Various other administrative data items relevant to the method call
￿
When a method is called, its entire stack frame is pushed onto the stack.
￿
When the method exits, its entire stack frame is popped from the stack. Popping a stack
frame is sometimes called unwinding the stack.
For example, the following code declares three methods. Main calls MethodA , which calls
MethodB , creating three stack frames. As the methods exit, the stack unwinds.
class Program
{
static void MethodA( int par1, int par2)
{
Console.WriteLine("Enter MethodA: {0}, {1}", par1, par2);
MethodB(11, 18); // Call MethodB.
Console.WriteLine("Exit MethodA");
}
static void MethodB(int par1, int par2)
{
Console.WriteLine("Enter MethodB: {0}, {1}", par1, par2);
Console.WriteLine("Exit MethodB");
}
static void Main( )
{
Console.WriteLine("Enter Main");
MethodA( 15, 30); // Call MethodA.
Console.WriteLine("Exit Main");
}
}
Search WWH ::




Custom Search