> 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
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!