Latest update

6/recent/ticker-posts

Call Stack

A call stack, in C#, is the list of names of methods called at run time from the beginning of a program until the execution of the current statement.


A call stack is mainly intended to keep track of the point to which each active subroutine should return control when it finishes executing. Call stack acts as a tool to debug an application when the method to be traced can be called in more than one context. This forms a better alternative than adding tracing code to all methods that call the given method. Whenever an exception is thrown anywhere in the user code, the Common Language Runtime (CLR) will unwind the call stack and search for the catch block to determine the specific exception type. If there is no appropriate handler, CLR will terminate the application. Call stack, therefore, is used to tell the execution pointer where to go next.

Call stack is organized as "stack," a data structure in memory for storing items in a last-in-first-out manner, so that the caller of the subroutine pushes the return address onto the stack and the called subroutine, after finishing, pops the return address off the call stack to transfer control to that address.

In C#, any application begins with a "main" method, which in turn calls other methods. On every call to a method, the method is added to the top of the stack and is removed from the stack on its return to the caller. Also, the scope of a variable declared in a block is determined from the time its value is pushed onto the stack (as part of call stack) until the execution leaves the block when the variable and the call stack are popped off the stack. Thus, the stack maintains both local variables (value types) and the call stack (stack frames), the size of which indicates the complexity of a program.

Post a Comment

0 Comments