RE: [ILUG] gcc optimisation weirdness?

From: Burke, Gary : GIG IS&T (Gary.Burke at domain guinness.com)
Date: Thu 11 May 2000 - 13:51:41 IST


]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);

Hmmm. int frequency[9]; give you elements 0 -> 8 inclusive. If input=10,
then you are trying to access frequency[10-1], or frequency[9], which is not
part of your array. What seems to be happening is that 'input' is being
places in memory where frequency[9] would be, if it existed. Hence,
'frequency[9]++' is basically being a 'input++', in this case.

Please note that by accessing an array out of bounds, you are in unknown
territory. Different compilers, and different options, will do things
differently. There is generally no way to know what you might be accessing.

Gary
**********************************************************************
This e-mail and any attachment contains information which is private
and confidential and is intended for the addressee only.
If you are not an addressee, you are not authorised to read, copy or
use the e-mail or any attachment.
If you have received this e-mail in error, please notify the sender
by return e-mail and then destroy it.
**********************************************************************



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:06:06 GMT