Pen*_*eng 5 recursion stack gcc return
我试图使用__builtin_return_addressGCC 中的函数来学习一些有关运行时数据结构的基本概念。
我的测试代码是这样的
#include <stdio.h>
void a(int i)
{
if (i>0) {
printf("The return address is %p\n", __builtin_return_address(0) );
a(--i);
}
else
return;
}
int main ()
{
a(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
The return address is 0x4005ee
The return address is 0x4005db
The return address is 0x4005db
The return address is 0x4005db
The return address is 0x4005db
...
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,为什么那些递归调用的函数的返回地址是一样的,就像它们都返回到顶级调用者一样?它们不应该是“递归直接调用者”之类的东西吗?
小智 3
f5 f4 f3 f2 f1 a(5) --> f5
f5 f4 f3 f2 f1 a(5) a(4) --> f3
f5 f4 f3 f2 f1 a(5) a(4) a(3) --> f1
f5 f4 f3 f2 f1 a(5) a(4) a(3) a(2) --> a(4)
f5 f4 f3 f2 f1 a(5) a(4) a(3) a(2) a(1) --> a(2)
Run Code Online (Sandbox Code Playgroud)
递归期间不要更改索引!
f5 f4 f3 f2 f1 a(5) --> f5
f5 f4 f3 f2 f1 a(5) a(5) --> f4
Run Code Online (Sandbox Code Playgroud)
停止于ret_add(i) == ret_add(0)