代码之家  ›  专栏  ›  技术社区  ›  pgod

如何在gcc中禁用tailcall优化

  •  12
  • pgod  · 技术社区  · 14 年前

    也就是说,在

     void main() {
         foo();
     }
    
     void foo() {
         bar();
     }
    
     void bar() {
         /* at this point in code, the foo() stack frame no longer exists! */    
     }
    

    当foo调用bar时,gcc会发出代码来替换foo的堆栈帧,而不是添加新的堆栈帧。

    我的公司有一个堆栈展开机,可以打印出堆栈跟踪从任何一点在代码。tailcall优化使堆栈帧消失,这会在一定程度上混淆堆栈跟踪。

    我正在使用gcc4.3为x86-64编译。

    提前谢谢! 第

    2 回复  |  直到 14 年前
        1
  •  32
  •   liori    14 年前

       -foptimize-sibling-calls
           Optimize sibling and tail recursive calls.
    
           Enabled at levels -O2, -O3, -Os.
    

    所以要么用 -O0 / -O1 -fno-optimize-sibling-calls .

        2
  •  13
  •   pmg    14 年前