I was told this is a buggy code, but I don’t know why, some could explain it to me.
why there would not be an array out of bound exception when I compile?
int a[10];
int j;
void main () {
int i, j = 42;
for (i = 0; i <=10; i++) a[i] = i;
printf("%d", j);
}
Here’s one bug:
should be
Another bug is in your loop. You write past the end of array
a, and if your compiler placedjin memory immediately followinga(which based on your question I assume it did), then the out-of-bounds array access will actually end up assigning a value toj. Hence, when you write10intoa[10](which doesn’t exist), you are writing it into the memory wherejlives (causing this to act likej = 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.