ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c-lang无限尾递归优化

2019-10-13 08:08:12  阅读:223  来源: 互联网

标签:c clang-2 tail-recursion


#include <iostream> 

int foo(int i){ 
     return foo(i + 1);
} 

int main(int argc,char * argv[]){ 
     if(argc != 2){ 
         return 1; 
     } 
     std::cout << foo(std::atoi(argv[1])) << std::endl; 
} 

%clang -O2 test.cc

%时间./a.out 42

1490723512

./a.out 42 0.00s用户0.00s系统69%cpu 0.004总计

%时间./a.out 42

1564058296

./a.out 42 0.00s用户0.00s系统56%cpu 0.006总计

%g -O2 test.cc

%./a.out 42#无限递归

^ C

% clang++ --version 
clang version 3.3 (tags/RELEASE_33/final) 
Target: x86_64-apple-darwin12.4.0 
Thread model: posix 
% g++ --version 
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) 
Copyright (C) 2007 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions.  There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

那么它是bug还是clang的功能?

解决方法:

尽管g和clang都能够编译C 98和C 11代码,但是clang从一开始就被设计为C 11编译器,并且在其DNA中嵌入了一些C 11行为(可以这么说).

有了C 11,C标准就可以识别线程了,这意味着现在有了一些特定的线程行为.特别是1.10 / 24指出:

The implementation may assume that any thread will eventually do one of the following:

— terminate,

— make a call to a library I/O function,

— access or modify a volatile object, or

— perform a synchronization operation or an atomic operation.

[Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. — end note ]

而这正是clang在优化时正在做的事情.可以看到该函数没有副作用,即使没有终止也将其删除.

标签:c,clang-2,tail-recursion
来源: https://codeday.me/bug/20191013/1906477.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有