Few points.
1. Shouldn't it be: int frequency[10];
2. Try to get assembler output for the various optimisation options and diff
them.
3. C compilers can init arrays @ 3 different times.
1. Compile time. Use static arrays of appropriate. All values are
set to 0
2. Program startup code, sets module level initialised arrays. I.E.
int arr[]={1,2};
3. Runtime code generated by compiler inits local initialised arrays
(using
the same construct as in 2).
p.s. a handy macro when working with arrays is
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
This will be worked out by the compiler at compile time.
So this tidys things a lot when you don't have to explicitly
specify (configure) the number of elements (99% of time).
e.g.
int arr[]={1,2,3,4,5};
int arr_idx=0;
for(;arr_idx<ARRAY_SIZE(arr);arr_idx++) printf("%d,",arr[arr_idx]);
> -----Original Message-----
> From: Paul Jakma [mailto:paulj at itg.ie]
> Sent: 10 May 2000 17:39
> To: ilug at linux.ie> Subject: [ILUG] gcc optimisation weirdness?
>>> 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!
>> seems like gcc is applying the increment to variable input,
> despite my bracketing of frequency[input-1]! which i don't
> want. why???
>> (even without the brackets it should not increment input)
>> --paulj.
>> PS: is there anyway to initialise an array at declaration time, rather
> than iterate thru the array and set each element manually? eg
> something
> like: int array[9]=0;
>>>> --
> Irish Linux Users' Group: ilug at linux.ie>http://www.linux.ie/mailman/listinfo/ilug for
> (un)subscription information.
> List maintainer: listmaster at linux.ie>
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!