我编写的 C++ 程序在使用优化(选项 -O1、-O2、-O3 等)编译时不起作用(出现分段错误),但是当我使用它时它运行良好无需优化即可编译.
I'm writing a C++ program that doesn't work (I get a segmentation fault) when I compile it with optimizations (options -O1, -O2, -O3, etc.), but it works just fine when I compile it without optimizations.
错误是否在我的代码中?或者我应该假设这是 GCC 中的一个错误?
Is there any chance that the error is in my code? or should I assume that this is a bug in GCC?
我的 GCC 版本是 3.4.6.
My GCC version is 3.4.6.
对于此类问题,是否有任何已知的解决方法?
Is there any known workaround for this kind of problem?
我的程序优化版和未优化版在速度上有很大差异,所以我真的需要使用优化.
There is a big difference in speed between the optimized and unoptimized version of my program, so I really need to use optimizations.
这是我原来的函子.一种在没有优化级别的情况下工作正常并在任何级别的优化中引发分段错误的方法:
This is my original functor. The one that works fine with no levels of optimizations and throws a segmentation fault with any level of optimization:
struct distanceToPointSort{
indexedDocument* point ;
distanceToPointSort(indexedDocument* p): point(p) {}
bool operator() (indexedDocument* p1,indexedDocument* p2){
return distance(point,p1) < distance(point,p2) ;
}
} ;
这个在任何级别的优化下都能完美运行:
And this one works flawlessly with any level of optimization:
struct distanceToPointSort{
indexedDocument* point ;
distanceToPointSort(indexedDocument* p): point(p) {}
bool operator() (indexedDocument* p1,indexedDocument* p2){
float d1=distance(point,p1) ;
float d2=distance(point,p2) ;
std::cout << "" ; //without this line, I get a segmentation fault anyways
return d1 < d2 ;
}
} ;
不幸的是,这个问题很难重现,因为它发生在一些特定的值上.在对一千多个向量中的一个进行排序时,我得到了分段错误,因此这实际上取决于每个向量具有的特定值组合.
Unfortunately, this problem is hard to reproduce because it happens with some specific values. I get the segmentation fault upon sorting just one out of more than a thousand vectors, so it really depends on the specific combination of values each vector has.
既然您发布了代码片段并找到了可行的解决方法(@Windows 程序员的回答),我可以说您正在寻找的是 -ffloat-store
.
Now that you posted the code fragment and a working workaround was found (@Windows programmer's answer), I can say that perhaps what you are looking for is -ffloat-store
.
-ffloat-store
-ffloat-store
不要在寄存器中存储浮点变量,并禁止其他可能改变浮点值是从寄存器还是内存中获取的选项.
Do not store floating point variables in registers, and inhibit other options that might change whether a floating point value is taken from a register or memory.
此选项可防止在 68000 等机器上出现不需要的超额精度,其中(68881 的)浮点寄存器保持比双精度更高的精度.x86 架构也是如此.对于大多数程序来说,额外的精度只会有好处,但少数程序依赖于 IEEE 浮点的精确定义.在修改这些程序以将所有相关的中间计算存储到变量后,将 -ffloat-store 用于此类程序.
This option prevents undesirable excess precision on machines such as the 68000 where the floating registers (of the 68881) keep more precision than a double is supposed to have. Similarly for the x86 architecture. For most programs, the excess precision does only good, but a few programs rely on the precise definition of IEEE floating point. Use -ffloat-store for such programs, after modifying them to store all pertinent intermediate computations into variables.
来源:http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Optimize-Options.html
这篇关于GCC:程序不适用于编译选项 -O3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!