Note that there are some explanatory texts on larger screens.

plurals
  1. POGCC: program doesn't work with compilation option -O3
    text
    copied!<p>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.</p> <p>Is there any chance that the error is in my code? or should I assume that this is a bug in GCC?</p> <p>My GCC version is 3.4.6.</p> <p>Is there any known workaround for this kind of problem?</p> <p>There is a big difference in speed between the optimized and unoptimized version of my program, so I really need to use optimizations.</p> <hr> <p>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:</p> <pre><code>struct distanceToPointSort{ indexedDocument* point ; distanceToPointSort(indexedDocument* p): point(p) {} bool operator() (indexedDocument* p1,indexedDocument* p2){ return distance(point,p1) &lt; distance(point,p2) ; } } ; </code></pre> <p>And this one works flawlessly with any level of optimization:</p> <pre><code>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 &lt;&lt; "" ; //without this line, I get a segmentation fault anyways return d1 &lt; d2 ; } } ; </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload