调用栈 (Call Stack)

调用栈作为程序执行的重要组成部分,用于保存返回地址、实现递归和局部变量存储。当子程序被调用时,返回地址会被推入栈中,执行完毕后弹出并返回。如果栈空间耗尽,则会导致栈溢出错误。此外,调用栈还用于传递参数和保存指向当前对象实例的指针,以及维护嵌套子程序的上下文。
摘要由CSDN通过智能技术生成
调用栈的英文叫做call stack,从其英文书名来看,知道它本身就是一个栈,故而它满足栈的先入后出的特性。


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.


关于call stack的作用,它也谈到了:

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.


存储指令的返回地址,是call 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 a

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值