You should explain what bug you see? What did you expect to happen, and what did you actually observe?
–
abelenkyJul 8 '10 at 23:26

1

The fact that you have j defined right after a, and after you overrun the array, you print j suggest to me that you perfectly well know what's happening. Or you need to phrase your question explicitly. In any case, whatever behavior you see from your program, it is not guaranteed to behave the same everywhere.
–
Alok SinghalJul 8 '10 at 23:29

3

"the" presumes there's only one bug in the code -- which is wrong.
–
Jerry CoffinJul 8 '10 at 23:31

1

Another pitfall is that you're doing what's called variable shadowing. The global variable j is not the same as the variable j declared in main(). I suggest you remove the global.
–
SiegeXJul 8 '10 at 23:38

hey guys, could we justify downvotes? so the user knows. I'm guessing it's because he's not defined the bug, which makes the answers useful, but the question meaningless...
–
andyJul 8 '10 at 23:44

5 Answers
5

Another bug is in your loop. You write past the end of array a, and if your compiler placed j in memory immediately following a (which based on your question I assume it did), then the out-of-bounds array access will actually end up assigning a value to j. Hence, when you write 10 into a[10] (which doesn't exist), you are writing it into the memory where j lives (causing this to act like j = 10). However, this behavior is dependent on how your compiler lays out the variables in memory, so you may very well see different behavior if you compiled the same program on a different platform.

There should not be an overflow of array, thats correct as you have mentioned in your question. But some compilers will give a PASS with the code whereas others will trigger a warning (or error is elevated).