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

Martin Guy martinwguy@yahoo.it
Sat May 3 08:54:00 GMT 2008


On 5/3/08, Benj FitzPatrick <benjfitz@yahoo.com> wrote:
> I forgot to include
>
>  return(integral);
>
>  My question is why does the buggy code give the
>  correct answer with no optimizations, but an incorrect
>  answer with optimizations turned on?

Because by chance, without optimisation, your "integral" variable
happens to be stored (or a copy of its value happens to be left) in
the same register or stack location as the default place for function
call results.  I would guess it's the first item on the stack while
the function is alive and the first item after the top of the stack
when it return.

   M



More information about the Gcc-help mailing list