yansheng.wang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

GCC编译选项--"-fno-strict-aliasing"

 (2009-08-09 14:31:45)
标签: 

杂谈

分类: 计算机与 Internet
    这两天编译别的组的代码。编译器是GCC4.1.2,发现使用优化选项O2以上代码执行的结果和使用O1(或不使用优化选项)不一样,使用O1编译出来的代码执行结果是正确的。上网搜索了一下,发现了原因。
    代码中有如下语句:
          float f = j;
          unsigned int* p = (unsigned int*)(&f);
    上述代码的第二句有强制类型转化,是出现问题的原因。原因及解决方法如下,是英文的:
 
    If optimization level is >= 2 in gcc-4.1, strict-aliasing is used, and this could cause probelms when a pointer is referencing to a different type of object and the object is referedthereafter by using this pointer. That is the case in this example. So you should force the compiler to not use strict-aliasing by a argument "-fno-strict-aliasing" if you want to use "-O2" or "-O3".
 
    看来以后得多学习一下GCC编译器的选项的意义了。
posted on 2012-11-21 14:22  小小程序员001  阅读(956)  评论(0编辑  收藏  举报