All Topics  
Call stack

 

   Email Print
   Bookmark   Link






 

Call stack



 
 
In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a call stack is a dynamic stack
Stack (data structure)

In computer science, a stack is an abstract data type and data structure based on the principle of LIFO . Stacks are used extensively at every level of a modern computer system....
 data structure that stores information about the active subroutine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
s of a computer program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
. This kind of stack is also known as an execution stack, control stack, function stack, or run-time stack, and is often shortened to just "the stack".

A call stack is often 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.






Discussion
Ask a question about 'Call stack'
Start a new discussion about 'Call stack'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a call stack is a dynamic stack
Stack (data structure)

In computer science, a stack is an abstract data type and data structure based on the principle of LIFO . Stacks are used extensively at every level of a modern computer system....
 data structure that stores information about the active subroutine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
s of a computer program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
. This kind of stack is also known as an execution stack, control stack, function stack, or run-time stack, and is often shortened to just "the stack".

A call stack is often 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. (The active subroutines are those which have been called but have not yet completed execution by returning.) If, for example, a subroutine DrawSquare calls a subroutine DrawLine from four different places, the code of DrawLine must have a way of knowing where to return. This is typically done by code for each call within DrawSquare putting the address
Memory address

In computer science, a memory address is an identifier for a computer memory location, at which a computer program or a hardware device can store a piece of data and later retrieve it....
 of the instruction
Instruction (computer science)

In computer science, an instruction is a single operation of a central processing unit defined by an instruction set architecture. In a broader sense, an "instruction" may be any representation of an element of an executable program, such as a bytecode....
 after the particular call statement
Statement (programming)

In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements....
 (the "return address") onto the call stack.

Since the call stack is organized as a stack
Stack (data structure)

In computer science, a stack is an abstract data type and data structure based on the principle of LIFO . Stacks are used extensively at every level of a modern computer system....
, the caller pushes the return address onto the stack, and the called subroutine, when it finishes, pops the return address off the call stack (and transfers control to that address). If a called subroutine calls on to yet another subroutine, it will push its return address onto the call stack, and so on, with the information stacking up and unstacking as the program dictates. If the pushing consumes all of the space allocated for the call stack, an error called a stack overflow
Stack overflow

In software, a stack overflow occurs when too much computer memory is used on the call stack. In many programming languages the call stack contains a limited amount of memory, usually determined at the start of the program....
 occurs. Adding a subroutine's entry to the call stack is sometimes called winding; conversely, removing entries is unwinding.

There is usually exactly one call stack associated with a running program (or more accurately, with each task
Task (computers)

A task is "an execution path through address space". In other words, a set of Computer program instruction s that are loaded in computer storage....
 or thread
Thread (computer science)

In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process....
 of a process
Process (computing)

In computing, a process is an Object of a computer program that is being sequentially executed by a computer system that has the ability to run several computer programs Concurrency ....
), although additional stacks may be created for signal
Signal (computing)

A signal is a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems. Essentially it is an asynchronous notification sent to a Process in order to notify it of an event that occurred....
 handling or cooperative multitasking (as with setcontext
Setcontext

setcontext is one of a family of C library Subroutines used for context control. The setcontext family allows the implementation in C of advanced control flow design patterns such as iterators, fiber , and coroutines....
). Since there is only one in this important context, it can be referred to as the stack (implicitly, "of the task").

In high-level programming language
High-level programming language

In computing, a high-level programming language is a programming language with strong Abstraction from the details of the computer. In comparison to low-level programming languages, it may use natural language elements, be easier to use, or more Porting across platforms....
s, the specifics of the call stack are usually hidden from the programmer. They are given access only to the list of functions, and not the memory on the stack itself. Most assembly language
Assembly language

An assembly language is a low-level language for programming computers. It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture....
s, on the other hand, require programmers to be involved with manipulating the stack. The actual details of the stack in a programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
 depend upon the compiler
Compiler

A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program....
, operating system
Operating system

An operating system is an interface between hardware and applications; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer....
, and the available instruction set
Instruction set

An instruction set is a list of all the instruction , and all their variations, that a processor can execute.Instructions include:* Arithmetic such as add and subtract...
.

Functions of the call stack

As noted above, the primary purpose of a call stack is:
  • Storing the return address – When a subroutine is called, the location of the instruction to return to needs to be saved somewhere. Using a stack to save the return address has important advantages over alternatives. One is that each task has its own stack, and thus the subroutine can be reentrant, that is, can be active simultaneously for different tasks doing different things. Another benefit is that recursion
    Recursion (computer science)

    Recursion is a way of thinking about and solving problems. In fact, Recursion_ is one of the central ideas of computer science. Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem....
     is automatically supported. 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. This capability is automatic with a stack.


A call stack may serve additional functions, depending on the language, operating system, and machine environment. Among them can be:
  • Local data storage – A subroutine frequently needs memory space for storing the values of local variable
    Local variable

    In computer science, a local variable is a variable that is given local scope . Such a variable is accessible only from the subroutine or statement block in which it is declared....
    s, the variables that are known only within the active subroutine and do not retain values after it returns. It is often convenient to allocate space for this use by simply moving the top of the stack by enough to provide the space. This is very fast to do compared with, say, a heap
    Dynamic memory allocation

    In computer science, dynamic memory allocation is the allocation of computer storage storage for use in a computer program during the runtime of that program....
     allocation. Note that each separate activation of a subroutine gets its own separate space in the stack for locals.


  • Parameter passing – Subroutines often require that values for parameters
    Parameter (computer science)

    In computer programming, a parameter is a special kind of variable#In_computer_programming that refers to data that a subroutine receives to operate on....
     be supplied to them by the code which calls them, and it is not uncommon that space for these parameters may be laid out in the call stack. Generally if there are only a few small parameters, processor register
    Processor register

    In computer architecture, a processor register is a small amount of Computer storage available on the CPU whose contents can be accessed more quickly than storage available elsewhere....
    s will be used to pass the values, but if there are more parameters than can be handled this way, memory space will be needed. The call stack works well as a place for these parameters, especially since each call to a subroutine, which will have differing values for parameters, will be given separate space on the call stack for those values.


  • Evaluation stack – Operands for arithmetic or logical operations are most often placed into register and operated on there. However, in some situations the operands may be stacked up to an arbitrary depth, which means something more than registers must be used. The stack of such operands, rather like that in an RPN calculator, is called an evaluation stack, and may occupy space in the call stack.


  • Pointer to current instance - Some object-oriented languages (e.g., C++
    C++

    C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
    ), store the this pointer
    This (computer science)

    In many object-oriented programming programming languages, this is a keyword that is used in instance methods to refer to the object on which they are working....
     along with function arguments in the call stack when invoking methods. The this pointer points to the object
    Object (computer science)

    In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
     instance associated with the method to be invoked. The this pointer is an essential part of the execution context in object oriented languages, since it provides access to member data and the V-Table
    Virtual method table

    A virtual method table, virtual function table, dispatch table, or vtable, is a mechanism used in programming language to support dynamic dispatch ....
     of the current object. The this pointer links layer
    Layer

    Layer may refer to:* A layer of archaeological deposits in an excavation* A layer hen, a hen raised to produce eggs* Stratum, a layer of rock or soil with internally consistent characteristics...
    s used in object-oriented design with layers (types of stack frames) of the run-time call stack.


  • Enclosing subroutine context - Some programming languages (e.g., Pascal
    Pascal (programming language)

    Pascal is an influential imperative programming and Procedural programming programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structure....
     and Ada
    Ada (programming language)

    Ada is a structured programming, statically typed, Imperative programming, and Object-oriented programming high-level language computer programming programming language, extended from Pascal and other languages....
    ) support nested subroutines, allowing an inner routine to access the context of its outer enclosing routine, i.e., the parameters and local variables within the scope of the outer routine. Such languages typically allow inner routines to call themselves recursively, resulting in multiple call stacks for the inner routines invocations, all of which point to the same outer routine context. This type of call frame is also known as a display.


  • Other return state – Besides the return address, in some environments there may be other machine or software states that need to be restored when a subroutine returns. This might include things like privilege level, exception handling information, arithmetic modes, and so on. If needed, this may be stored in the call stack just as the return address is.


The typical call stack is used for the return address, locals, and parameters (known as a call frame). In some environments there may be more or fewer functions assigned to the call stack. In the Forth programming language, for example, only the return address and local variables are stored on the call stack (which in that environment is named the return stack); parameters are stored on a separate data stack. Most Forths also have a third stack for floating point
Floating point

In computing, floating point describes a system for numerical representation in which a String of digits represents a rational number.The term floating point refers to the fact that the radix point can "float": that is, it can be placed anywhere relative to the Significant figures of the number....
 parameters.

Structure

A call stack is composed of stack frames (sometimes called activation records). These are machine dependent data structures containing subroutine state information. Each stack frame corresponds to a call to a subroutine which has not yet terminated with a return. For example, if a subroutine named DrawLine is currently running, having just been called by a subroutine DrawSquare, the top part of the call stack might be laid out like this (where the stack is growing towards the top): The stack frame at the top of the stack is for the currently executing routine. In the most common approach the stack frame includes space for the local variables of the routine, the return address back to the routine's caller, and the parameter values passed into the routine. The stack is often accessed via a register called the stack pointer, which also serves to indicate the current top of the stack. Alternatively, memory within the frame may be accessed via a separate register, often termed the frame pointer, which typically points to some fixed point in the frame structure, such as the location for the return address.

Stack frames are not all the same size. Different subroutines have differing numbers of parameters, so that part of the stack frame will be different for different subroutines, although usually fixed across all activations of a particular subroutine. Similarly, the amount of space needed for local variables will be different for different subroutines. In fact, some languages support dynamic allocations of memory for local variables on the stack, in which case the size of the locals area will vary from activation to activation of a subroutine, and is not known when the subroutine is compiled
Compiler

A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program....
. In the latter case, access via a frame pointer, rather than via the stack pointer, is usually necessary since the offsets from the stack top to values such as the return address would not be known at compile time.

In many systems a stack frame has a field to contain the previous value of the frame pointer register, the value it had while the caller was executing. For example, in the diagram above, the stack frame of DrawLine would have a memory location holding the frame pointer value that DrawSquare uses. The value is saved upon entry to the subroutine and restored for the return. Having such a field in a known location in the stack frame allows code to access each frame successively underneath the currently executing routine's frame.

Programming languages that support nested subroutines
Nested function

In computer programming, a nested function is a subroutine which is lexically encapsulated within another function. It can only be called by the enclosing function or by functions directly or indirectly nested within the same enclosing function....
 have a field in the call frame that points to the call frame of the outer routine that invoked the inner (nested) routine. This is sometimes called a display. This pointer provides the inner routine (as well as any other inner routines it may invoke) access to the parameters and local variables of the outer invoking routine. A few computers, such as the Burroughs large systems, have special "display registers" to support such nested functions.

For some purposes, the stack frame of a subroutine and that of its caller can be considered to overlap, the overlap consisting of the area where the parameters are passed from the caller to the callee. In some environments, the caller pushes each argument onto the stack, thus extending its stack frame, then invokes the callee. In other environments, the caller has a preallocated area at the top of its stack frame to hold the arguments it supplies to other subroutines it calls. This area is sometimes termed the outgoing arguments area or callout area. Under this approach, the size of the area is calculated by the compiler to be the largest needed by any called subroutine.

Use


Call site processing

Usually the call stack manipulation needed at the site of a call to a subroutine is minimal (which is good since there can be many call sites for each subroutine to be called). The values for the actual arguments are evaluated at the call site, since they are specific to the particular call, and either pushed onto the stack or placed into registers, as determined by the calling convention
Calling convention

In computer science, a calling convention is a scheme for how function s receive parameters from their caller and how they return a result; calling conventions can differ in:...
 being used. The actual call instruction, such as "Branch and Link," is then typically executed to transfer control to the code of the target subroutine.

Callee processing

In the called subroutine, the first code executed is usually termed the subroutine prologue
Function prologue

In assembly language Computer programming, the function prologue is a few lines of code which appear at the beginning of a function, which prepare the Call stack and Processor register for use within the function....
, since it does the necessary housekeeping before the code for the statements of the routine is begun.

The prologue will commonly save the return address left in a register by the call instruction by pushing the value onto the call stack. Similarly, the current stack pointer and/or frame pointer values may be pushed. Alternatively, some instruction set architectures automatically provide comparable functionality as part of the action of the call instruction itself, and in such an environment the prologue need not do this.

If frame pointers are being used, the prologue will typically set the new value of the frame pointer register from the stack pointer. Space on the stack for local variables can then be allocated by incrementally changing the stack pointer.

The Forth programming language allows explicit winding of the call stack (called there the "return stack"). The Scheme programming language allows the winding of special frames on the stack through a "dynamic wind".

Return processing

When a subroutine is ready to return, it executes an epilogue that undoes the steps of the prologue. This will typically restore saved register values (such as the frame pointer value) from the stack frame, pop the entire stack frame off the stack by changing the stack pointer value, and finally branch to the instruction at the return address. Under many calling conventions the items popped off the stack by the epilogue include the original argument values, in which case there usually are no further stack manipulations that need to be done by the caller. With some calling conventions, however, it is the caller's responsibility to remove the arguments from the stack after the return.

Unwinding

Returning from the called function will pop the top frame off of the stack, perhaps leaving a return value.

Some languages (such as Pascal) allow a global goto
GOTO

GOTO is a statement found in many computer programming languages. It is a combination of the English words wiktionary:go and wiktionary:to....
 statement to transfer control out of a nested function and into a previously invoked outer function. This operation requires the stack to be unwound, removing as many stack frames as necessary to restore the proper context to transfer control to the target statement within the enclosing outer function. Such transfers of control are generally used only for error handling.

Other languages (such as Object Pascal
Object Pascal

Object Pascal refers to a branch of Object-oriented programming derivatives of Pascal , mostly known as the primary programming language of CodeGear Delphi....
) provide exception handling
Exception handling

Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions - special conditions that change the normal flow of execution....
, which also requires unwinding of the stack. The stack frame of a function contains one or more entries specifying exception handlers. When an exception is thrown, the stack is unwound until an exception handler is found that is prepared to handle (catch) the exception. Common Lisp
Common Lisp

Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
 allows control of what happens when the stack is unwound by using the unwind-protect special operator.

When applying a continuation
Continuation

In computing and programming, a continuation is an abstract representation of Control flow, or the "rest of computation" or "rest of code to be executed"....
, the stack is unwound and then rewound with the stack of the continuation. This is not the only way to implement continuations; for example, using multiple, explicit stacks, application of a continuation can simply activate its stack and wind a value to be passed.

Call Stacks and Software Testing

A recently reported technique uses call stacks in a very different way than others discussed on this page. It uses call stacks for test suite reduction. Briefly, Test suite reduction seeks to reduce the number of test cases in a test suite while retaining a high percentage of the original suite’s fault detection effectiveness. Two test cases are considered to be equivalent if they generate the same set of call stacks during execution. See for more details.

Performance analysis

Taking random-time samples of the call stack can be very useful in optimizing performance of programs. The reason is if a subroutine call instruction appears on the call stack for a certain fraction of execution time, its possible removal would save that much time. See Performance analysis
Performance analysis

In software engineering, performance analysis, more commonly today known as profiling, is the investigation of a program's behavior using information gathered as the program executes ....
 and Deep sampling
Deep sampling

Deep sampling is a variation of statistical sampling in which precision is sacrificed for insight. Small numbers of samples are taken, with each sample containing much information....
.

Security

The mixing of control flow data affecting the execution of code (return addresses, saved frame pointers) and simple program data (parameters, return values) in a call stack is a security risk, possibly exploit
Exploit (computer security)

An exploit is a piece of software, a chunk of data, or sequence of commands that take advantage of a software bug, glitch or vulnerability in order to cause unintended or unanticipated behavior to occur on computer software, hardware, or something electronic ....
able through buffer overflow
Buffer overflow

In computer security and computer programming, a buffer overflow, or buffer overrun, is an Anomaly in software condition where a process attempts to store data beyond the boundaries of a fixed-length buffer ....
s (in which article the risk and exploitation are explained).

See also

  • Dynamic memory allocation
    Dynamic memory allocation

    In computer science, dynamic memory allocation is the allocation of computer storage storage for use in a computer program during the runtime of that program....
  • Automatic memory allocation
    Automatic memory allocation

    In computer programming, an automatic variable is a scope variable which is allocated and de-allocated automatically when program flow enters and leaves the variable's scope ....
  • Calling convention
    Calling convention

    In computer science, a calling convention is a scheme for how function s receive parameters from their caller and how they return a result; calling conventions can differ in:...
  • Stack buffer overflow
    Stack buffer overflow

    In software, a stack buffer overflow occurs when a program writes to a computer memory address on the program's call stack outside of the intended data structure; usually a fixed length buffer....


External links

  • on MSDN