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

来源:互联网 发布:goeasy实现java推送 编辑:程序博客网 时间:2024/06/05 19:18

文中的编码方法绝对要禁止用在项目中, 最近用开源的代码时碰到过需要这个选项的警告

这两天编译别的组的代码。编译器是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 refered there after 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".

0 0