调用栈(call stack)

调用栈(call stack)是一个重要的程序运行机制,遵循先进后出的规则。它用于存储返回地址、局部变量、参数传递等功能,支持递归调用和线程独立的调用上下文。调用栈的溢出可能导致程序崩溃,而栈帧(stack frames)则根据函数的不同需求变化大小。通过帧指针和栈指针访问栈数据,某些语言如C++会使用栈来存储this指针。此外,调用栈在处理闭包和静态嵌套子程序时也有特殊作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用栈的英文叫做call stack,从其英文书名来看,知道它本身就是一个栈,故而它满足栈的先入后出的特性。

wiki上有篇文章讲述call stack:http://en.wikipedia.org/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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值