GCC: program doesn't work with compilation option -O3

后端 未结 16 1613
感动是毒
感动是毒 2020-12-16 20:12

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

16条回答
  •  醉酒成梦
    2020-12-16 20:40

    Alright... This is one of the weirdest problems I've ever had.
    I dont think I have enough proof to state it's a GCC bug, but honestly... It really looks like one.

    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.

提交回复
热议问题