Benj FitzPatrick wrote:
Hello there,
I wrote a program that numerically integrates a morse
potential, given starting and ending points, using
simpsons rule. It turns out I forgot to include
return(integral);
at the end of the function. What I noticed was that
if I used gcc -O0 the program would work fine, but if
I used any level of optimization it would give near
zero for the integral when it should be between 1 and
9. Also, if I put in a printf("%8.3e\n", integral) at
the end the program gives the correct answers when
optimizations are turned on.
My question is why does the buggy code give the
correct answer with no optimizations, but an incorrect
answer with optimizations turned on?
_________________________________________________
The un-optimized code apparently happens to calculate the result in the same register where the calling function expects it. You certainly can't count on this. The effect would depend on target identity as well as optimization level, as you have already been reminded.