对于怀疑在“gdb”的尾巴优化code [英] Doubt regarding a tail optimized code under 'gdb'
问题描述
考虑在C尾递归阶乘实现:
Consider a tail recursive factorial implementation in C:
#include <stdio.h>
unsigned long long factorial(unsigned long long fact_so_far, unsigned long long count, unsigned long long max_count){
if (max_count==0 || max_count==1 || count >= max_count)
return fact_so_far;
else
{
printf("%llu %p \n", count, &factorial);
return factorial(fact_so_far * count, ++count, max_count);
}
}
int main(int argc, char **argv)
{
unsigned long long n;
scanf("%llu", &n);
printf("\n Factorial %llu \n",factorial(1,0,n));
return 0;
}
我放在'因子'断点,我在gdb的执行上面。断点是永远不会命中。
I place a breakpoint in 'factorial' and I run the above under 'gdb'. The breakpoint is never hit.
假设它的尾部调用优化(我用gcc编译-O2它),它应该命中断点,一旦ATLEAST,IIRC。
Assuming that its tail call optimised (I have compiled it using gcc -O2), it should hit the breakpoint, atleast once, IIRC.
编辑:我得到的最终结果没有击中任何断点。对于如
I get the final result without hitting any breakpoint. For eg,
(gdb) b factorial
Breakpoint 1 at 0x8048429: file factorial-tail.c, line 3.
(gdb) run
Starting program: /home/amit/quest/codes/factorial-tail
5
0 0x8048420
1 0x8048420
2 0x8048420
3 0x8048420
4 0x8048420
Factorial 120
Program exited normally.
(gdb)
我在哪里去了?
推荐答案
这可能是阶乘函数内联到主。如果发生这种情况,将会有用于从其他.c文件调用阶乘的第二个副本;这就是你的断点。尝试通过-fno内联。
It could be that the factorial function is inlined into main. If this happens, there will be a second copy of factorial used for calls from other .c files; that's where your breakpoint was. Try passing -fno-inline.
这篇关于对于怀疑在“gdb”的尾巴优化code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!