Content deleted Content added
→Practical implications: More precise about Java. |
Add {{citation-needed}} for the assertion that heap allocation of activation frames is less efficient than stack allocation |
||
Line 8:
When one function calls another during a typical program's execution, the local state of the caller (including [[parameter (computer science)|parameter]]s and [[local variable]]s) must be preserved in order for execution to proceed after the callee returns. In most compiled programs, this local state is stored on the [[call stack]] in a data structure called a ''[[Call stack#Structure|stack frame]]'' or ''activation record''. This stack frame is pushed, or allocated, as prelude to calling another function, and is popped, or deallocated, when the other function returns to the function that did the call. The upwards funarg problem arises when the calling function refers to the called/exited function's state after that function has returned. Therefore, the stack frame containing the called function's state variables must not be deallocated when the function returns, violating the [[stack-based memory allocation|stack-based function call paradigm]].
One solution to the upwards funarg problem is to simply allocate all stack frames from the [[dynamic memory allocation#Heap-based memory allocation|heap]] instead of the stack, and rely on some form of [[Garbage collection (computer science)|garbage collection]] or [[reference counting]] to deallocate the stack frames when they are no longer needed. Managing stack frames on the heap is much less efficient than on the stack {{citation-needed}}, so this strategy may significantly degrade performance. Moreover, because most functions in typical programs do not create upwards funargs, much of this overhead is unnecessary.
Some efficiency-minded compilers employ a hybrid approach in which the stack frames for a function are allocated from the stack if the compiler is able to deduce, through [[static program analysis]], that the function creates no upwards funargs. Otherwise, the stack frames are allocated from the heap.
|