[ILUG] gcc optimisation weirdness?

Frank Peelo fpeelo at eircom.net
Wed May 10 18:18:22 IST 2000


> gcc seems to do weird things to the following:
>
> int input, frequency[9];
>
> //frequency[] initialised to 0.
>
> do {
>                 scanf("%d", &input);
>                 if (input > 0 && input < 11) {
> //point 1
>                         (frequency[input-1])++;
> //point 2
>                         }
>         } while (input);
>
> if i compile with -O2 or -O3 it works as i expect it to, but if i compile
> with "gcc -o test test.c" then:
>
> it works correctly for digits 0 thru 9, however if i enter 10 it goes
> wrong: at point 1 input == 10, at point 2 input == 11!

Sorry if this reply is totally daft; I haven't used gcc, nor have I
programmed on Linux/Unix. But I wonder how you determined the value of input
at the two points? I saw an optimising compiler, where variables could
sometimes be shuffled around in a way that could confuse the debugger, so it
occurs to me that either
a) the debugger may not be telling the truth, or
b) the compiler may have determined that it does not need to know the value
of input and has therefore discarded it. (It needs to know whether or not
input was !=0 but that's all; could that condition be set in the carry flag
by the if statement? 8086 inc doesn't affect carry.)

Frank






More information about the ILUG mailing list