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

后端 未结 16 1610
感动是毒
感动是毒 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:56

    The true answer is hidden somewhere inside all the comments in this thread. First of all: it is not a bug in the compiler.

    The problem has to do with floating point precision. distanceToPointSort should be a function that should never return true for both the arguments (a,b) and (b,a), but that is exactly what can happen when the compiler decides to use higher precision for some data paths. The problem is especially likely on, but by no means limited to, x86 without -mfpmath=sse. If the comparator behaves that way, the sort function can become confused, and the segmentation fault is not surprising.

    I consider -ffloat-store the best solution here (already suggested by CesarB).

提交回复
热议问题