System stack: Difference between revisions

Stack frame added, stack use in high-integrity systems, cases when multiple stacks used
(More should probably be added to this...)
 
(Stack frame added, stack use in high-integrity systems, cases when multiple stacks used)
Line 1:
[[Category:Encyclopedia]]The '''system stack''' (a.k.a. '''call stack''' or just "the stack") is a place in memory for things that the [[heap]] doesn't cover. The system stack is more organized than the heap since it uses the [[stack]] data structure, where order matters. Also, the address of the next allocation is known at all times because of this organization. Allocated items are pushed on to the stack in a particular order and popped off when needed.
 
Most importantly, the system stack is used to store information about subroutine calls (where it gets the name "call stack"). The stack stores parameters for the function and a return address where the program should pick up when the function is finished. It also reserves a space for a return value to be popped by the system on return. BecauseThe piece of itsstack limitedused size,by a stack may "overflow" if too many function calls are made without returning. This situationsubprogram is dangerous because, if not handled properly (usually by the program stopping and freeing all of its memory), thecalled '''stack could intersect and overwrite other memory from the program, other programs, or the [[operating system]]frame'''.
 
Because of its limited size, a stack may "overflow" if too many function calls are made without returning. This situation is dangerous because, if not handled properly (usually by the program stopping and freeing all of its memory), the stack could intersect and overwrite other memory from the program, other programs, or the [[operating system]]. In high-integrity systems one of strict design requirements is predictability of the stack size. Use of [[heap]] is usually prohibited, for the same reason.
 
Often there exist more than one ''stack'':
 
* In [[concurrent programming]] each [[task]] has a ''stack'' of its own. Differently to the [[heap]] ''stack'' need not to be shared between the [[task]]s and thus no interlocking is required;
* The language run-time environment may maintain multiple ''stacks'' for one [[task]]. For example, the arguments and the local variables of a subprogram may be allocated on a stack different from the stack used for the return. When the subprograms may return variable size values this prevents copying of those. Upon return stacks are swapped;
* Secondary ''stacks'' may be used for allocation on non-contiguous objects, typically strings.