GCC:程序不能与编译选项-O3一起工作 [英] GCC: program doesn't work with compilation option -O3

查看:549
本文介绍了GCC:程序不能与编译选项-O3一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写一个C ++程序,当我用优化(选项-O1,-O2,-O3等)编译它不工作(我得到一个分段错误),但它工作很好,当我编译它没有优化。



有错误在我的代码有任何机会吗?或者我应该假设这是GCC中的错误吗?



我的GCC版本是3.4.6。



这种问题有什么已知的解决方法吗?



我的程序的优化和未优化版本之间的速度有很大的区别,所以我真的需要使用优化。






这是我原来的函子。一个工作正常,没有任何水平的优化和抛出一个分段错误与任何级别的优化:

  struct distanceToPointSort {
indexedDocument * point;
distanceToPointSort(indexedDocument * p):point(p){}
bool operator()(indexedDocument * p1,indexedDocument * p2){
return distance(point,p1)距离(点,p2);
}
};

这个工具完美无缺地与任何级别的优化:

  struct distanceToPointSort {
indexedDocument * point;
distanceToPointSort(indexedDocument * p):point(p){}
bool operator()(indexedDocument * p1,indexedDocument * p2){

float d1 = p1);
float d2 = distance(point,p2);

std :: cout<< ; //没有这行,我得到一个分段错误无论如何

return d1< d2;
}
};不幸的是,这个问题很难重现,因为它发生在一些特定的值。

解决方案

/ div>

现在你发布了代码片段,并找到一个工作的解决方法(@Windows程序员的答案),我可以说,你可能要找的是 -ffloat-store


-ffloat-store



并禁止可能改变是否从寄存器或存储器中获取浮点值的其他选项。



此选项可防止机器上出现不必要的过度精度,例如68000其中(68881的)浮动寄存器保持比双精度更精确。类似地对于x86架构。对于大多数程序,超额精度只是好的,但几个程序依赖于IEEE浮点的精确定义。对这些程序使用-float-store,在修改它们以将所有相关的中间计算存储到变量中之后。


资料来源: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Optimize-Options.html


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.

Is there any chance that the error is in my code? or should I assume that this is a bug in GCC?

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.

解决方案

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

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.

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.

Source: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Optimize-Options.html

这篇关于GCC:程序不能与编译选项-O3一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆