LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] gcc optimisation weirdness?

[ILUG] gcc optimisation weirdness?

Mark Fallon mfallon at ie.oracle.com
Wed May 10 23:41:03 IST 2000


Paul Jakma wrote:
> 
> 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.
> 

Hi,

I may be reading this incorrectly but, you declare a nine
integer array which means that you can access elements 0..8.
You then use 9 (10 -1) as an index to that array, so ou have gone
out of bounds.

What is probably happening is that gcc has arranged your variables
in memory differently for the optimised version and you only see 
thr bad behaviour when you compile without optimisation.

Regards,

Mark
_______________________________________________________________

Mark Fallon			E-mail : mfallon at ie.oracle.com
Senior Software Engineer	Phone  : +353-1-8033207
Product Line Engineering	Fax    : +353-1-8033221
_______________________________________________________________




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


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!
RSS Version
Powered by Dell