gcc 4.2.2: unexpected difference in behavior between -O0 and -Ox for x = 1, 2, 3

Robert Kiesling g6522c@earthlink.net
Sat May 3 02:31:00 GMT 2008



>Benj FitzPatrick <benjfitz@yahoo.com>
>
>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?

Code generated without optimization follows a basic function
call protocol.  The protocols depend on a particular platform, 
and they include aspects of the function calling conventions
like for things like how function arguments on the stack, and, 
in this particular case, how registers get allocated and how the
function call returns a value. 

The particular protocol depends on the platform, but when you
tell the compiler to optimize the generated code, and your 
program can no longer depend on, for example, a register being
allocated to store the return value of a function.

A look at the generated assembly code from the program would 
probably provide the exact answer.

Robert Kiesling





---------------------------------------------------
Ctalk Home Page: http://www.ctalklang.org/.



More information about the Gcc-help mailing list