![]() ![]() Local data storage A subroutine frequently needs memory space for storing the values of local variables, the variables that are known only within the active subroutine and do not retain values after it returns. Stack structures provide this capability automatically.ĭepending on the language, operating system, and machine environment, a call stack may serve additional purposes, including for example: When a function calls itself recursively, a return address needs to be stored for each activation of the function so that it can later be used to return from the function activation. Another benefit is that by providing reentrancy, recursion is automatically supported. One is that each task can have its own stack, and thus the subroutine can be thread-safe, that is, can be active simultaneously for different tasks doing different things. Using a stack to save the return address has important advantages over some alternative calling conventions, such as saving the return address before the beginning of the called subroutine or in some other fixed location. When a subroutine is called, the location (address) of the instruction at which the calling routine can later resume needs to be saved somewhere. The actual details of the stack in a programming language depend upon the compiler, operating system, and the available instruction set.Īs noted above, the primary purpose of a call stack is to store the return addresses. Most assembly languages, on the other hand, require programmers to be involved with manipulating the stack. ![]() They are given access only to a set of functions, and not the memory on the stack itself. In high-level programming languages, the specifics of the call stack are usually hidden from the programmer. Since there is only one in this important context, it can be referred to as the stack (implicitly, "of the task") however, in the Forth programming language the data stack or parameter stack is accessed more explicitly than the call stack and is commonly referred to as the stack (see below). There is usually exactly one call stack associated with a running program (or more accurately, with each task or thread of a process), although additional stacks may be created for signal handling or cooperative multitasking (as with setcontext). Adding a subroutine's entry to the call stack is sometimes called "winding" conversely, removing entries is "unwinding". If the pushing consumes all of the space allocated for the call stack, an error called a stack overflow occurs, generally causing the program to crash. If a called subroutine calls on yet another subroutine, it will push another return address onto the call stack, and so on, with the information stacking up and unstacking as the program dictates. Since the call stack is organized as a stack, the caller pushes the return address onto the stack, and the called subroutine, when it finishes, pulls or pops the return address off the call stack and transfers control to that address. To accomplish this, the address following the instruction that jumps to DrawLine, the return address, is pushed onto the top of the call stack with each call. For example, if a subroutine DrawSquare calls a subroutine DrawLine from four different places, DrawLine must know where to return when its execution completes. Such activations of subroutines may be nested to any level (recursive as a special case), hence the stack structure. An active subroutine is one that has been called, but is yet to complete execution, after which control should be handed back to the point of call. Many computer instruction sets provide special instructions for manipulating stacks.Ī call stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing. Although maintenance of the call stack is important for the proper functioning of most software, the details are normally hidden and automatic in high-level programming languages. ![]() This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or machine stack, and is often shortened to just " the stack". In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program.
0 Comments
Leave a Reply. |