Call stack

This product was added to computer science because of the content, defects on the quality assurance side of the editor. This is done to bring the quality of the articles from the computer science subject area to an acceptable level. Help us to eliminate the substantive shortcomings of this article and take part you in the discussion! ( ) Reason: Expandable beginning of the article - Lord van Tasm « ₪ » ‣ P: MB 15:33, August 22, 2012 (UTC)

Under a call stack (English callstack called ) is referred to in software engineering and computer science, a particularly unused stack memory, information which is being called subroutines holds up to the duration of a program. These usually the special processor register stack pointer is used. The instruction set of such a processor is instructions are for setting up, filling and disassembly of the stack as well as commands for entry into a subroutine and return from a subroutine.

Description

In the application of structured programming, a number of small sub-programs are formed to implement a particular functionality each exactly once. They should be accessible from different locations. These addresses must be stored where the execution will continue after the subroutine when calling subroutines used. This is the address that is directly behind the address of the subroutine call. If the end of the subroutine reaches during execution, so the further execution is therefore dependent on the location that called the subroutine ( call level). For this purpose the previously saved return address will be removed again from the call stack and loaded into the processor instruction pointer register. This method can be called nested subroutines, as long as the call stack provides this space.

Be set up in the use of processes and threads must for every process and thread has its own call stack, so return addresses and local variables do not overwrite each other.

The three main tasks of the call stack are providing parameter variables for called subprograms, storing the return address to continue after the end of the subroutine program execution at the right place ( after the subroutine call), and the provision of local variables of subroutines.

From the filling and emptying the call stack, however, you get a programmer usually nothing to do with when programming in high level languages ​​such as Java. Then generate the necessary compiler commands to use the call stack. After completion of a subroutine call stack must back the original, have level ', so that a subroutine can be called any number of times without the call stack overflow or underflow learns.

On the next free call stack entry stack pointer is commonly referred to by the special processor registers.

Function

Call parameters

Subroutines can be dependent on parameter variables. The values ​​are packed before the call to the call stack and can be used within the subroutine. Number, data type, calling convention and order of the parameters must be coordinated between the caller and the subroutine what is being communicated through signatures in the declaration, for example. Alternatively, parameters can be passed in processor registers.

Local variables

Place Uses a subroutine local variables, it is allocated on the call stack for them. Since each call results in a separate set of variables so that recursive subroutine calls are possible, that is, the younger Views do not change the variables of older, they are still running subroutine calls during execution. When using recursive calls always abort conditions have to guarantee a limited nesting, otherwise an overflow of the call stack is the result.

For access to local variables of the frame pointer (also pointer ( computer science ) English frame pointer, see ) is often used which points to the address in the call stack associated with the return address.

Return Values

Result values ​​of subroutines can also be returned through the call stack. This is the case for specific calling conventions. Then occupy the same block as the call parameters. The size of this area must therefore be chosen such that each of the two types of fits. When you return the call parameters are no longer needed, so the return value will simply overwrite the call parameters and available when you return available. However, since one execution thread can always return only one call level, it is sufficient that return values ​​in a reserved, static memory area to accommodate. This is the predominant method. The return values ​​of all subroutines then share this memory area.

Shaping

For each active subroutine call so there is the following structure (also called a stack frame ):

  • Optional input parameters ( return values ​​divided with optional generated later)
  • Return address
  • Optional local variables

After a subroutine call has the following structure:

  • Optional return value of the subroutine

The following diagram shows three exemplary call levels are shown.

Call state of a program

In the event of an unrecoverable error, a so-called stack trace is seen most often for diagnostic purposes when an exception. This edition reflects the call stack resist at the time of the error.

Safety considerations

The proximity of local variables to the return address may allow a compromise of the software by buffer overflows.

If it is possible malicious software in the just the called subprogram stored on the call stack return address defined to override, so she can control what code is executed after the return. Has been previously stored to own malicious code in main memory, it can be passed to the malicious code with this method of control.

If, for example, a buffer under the local variables, and manages the external malicious software to achieve by exploiting programming errors that this buffer is described by its defined end addition, the location of the return address is reachable. Because the call stack is typically (but the buffer from lower to higher addresses ) described from higher to lower addresses are by overwriting the buffer older contents of the call stack variable (see also buffer overflow).

History

In McCarthy, the use of call stacks at a LISP implementation from 1958 is reported.

Documents

  • Data structure
  • Recursion
  • Subroutine
87746
de