wiki上有篇文章讲述 call stack。
关于栈的溢出(stack overflow),有下面的定义:
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, 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 another 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 occurs, generally causing the program to crash.
Storing the return address When a subroutine is called, the location (address) of the instruction at which it can later resume 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 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.
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. It is often c

调用栈作为程序执行的重要组成部分,用于保存返回地址、实现递归和局部变量存储。当子程序被调用时,返回地址会被推入栈中,执行完毕后弹出并返回。如果栈空间耗尽,则会导致栈溢出错误。此外,调用栈还用于传递参数和保存指向当前对象实例的指针,以及维护嵌套子程序的上下文。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



