int testFun(int A)
{
return A+1;
}
int main()
{
int x=0;
int y= testFun(x)
cout<<y;
}
正如我们所知,堆栈保存局部变量,这意味着当我在主函数中时,堆栈中有变量(x 和 y),而当我调用函数(testFun)时,堆栈中有变量(A) 当我从 (testFun) 返回时,堆栈弹出最后一帧 但是这里的问题是,当我从 (testFun) 返回时,它如何知道在调用 (testFun) 之前它在主函数中的最后一个位置
最佳答案
when I return from (testFun), how it know the last place it were in the main function before calling the (testFun)
编译器解析代码并生成在 CPU 上运行的机器指令。函数调用产生一条 CALL
指令。当函数退出时,RET
指令用于返回调用者。
CALL
指令将 后面 CALL
本身的指令的地址压入调用堆栈,然后跳转到的起始地址指定的函数。
RET
指令从调用堆栈弹出该地址,然后跳转到指定地址。
关于c++ - 我怎么知道我上次的指示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58253720/