GCC编译选项--"-fno-strict-aliasing"
2016-10-06 13:49
435 查看
文中的编码方法绝对要禁止用在项目中,
最近用开源的代码时碰到过需要这个选项的警告
这两天编译别的组的代码。编译器是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".
最近用开源的代码时碰到过需要这个选项的警告
这两天编译别的组的代码。编译器是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".
相关文章推荐
- GCC编译选项--"-fno-strict-aliasing"
- GCC编译选项--"-fno-strict-aliasing"
- 善用"Assume No Aliasing"编译选项
- 编译告警处理:dereferencing type-punned pointer will break strict-aliasing rules
- GCC "-fomit-frame-pointer"编译选项的含义
- GCC strict aliasing
- gcc的 "-fpack-struct" 编译选项导致程序core dump的分析
- gcc警告: warning: dereferencing type-punned pointer will break strict-aliasing rules
- GCC strict aliasing – 嫉妒就是承认自己不如别人
- 消除由于使用gcc编译选项-Wunused-parameter而导致的 warning: "unused parameter xxxx"警告
- MySQL编译选项 -fno-strict-aliasing随手记
- Gcc编译选项-fno-builtin -fno-builtin-function
- 【GCC】gcc编译参数之-fno-strict-aliasing
- [GCC for C]编译选项---IDE掩盖下的天空
- GCC使用技巧之编译选项
- 结合"hello world"探讨gcc编译程序的过程
- [GCC for C]编译选项---IDE掩盖下的天空
- [GCC for C++]编译选项---IDE掩盖下的天空
- warning: dereferencing type-punned pointer will break strict-aliasing rules(转)
- [GCC for C]编译选项---IDE掩盖下的天空