I have recently started to learn C and I am taking a class with C as the subject. I'm currently playing around with loops and I'm running into some odd behaviour which I don't know how to explain.
#include <stdio.h>
int main()
{
int array[10],i;
for (i = 0; i <=10 ; i++)
{
array[i]=0; /*code should never terminate*/
printf("test \n");
}
printf("%d \n", sizeof(array)/sizeof(int));
return 0;
}
On my laptop running Ubuntu 14.04, this code does not break. It runs to completion. On my school's computer running CentOS 6.6, it also runs fine. On Windows 8.1, the loop never terminates.
What's even more strange is that when I edit the condition of the for
loop to: i <= 11
, the code only terminates on my laptop running Ubuntu. It never terminates in CentOS and Windows.
Can anyone explain what's happening in the memory and why the different OSes running the same code give different outcomes?
EDIT: I know the for loop goes out of bounds. I'm doing it intentionally. I just can't figure out how the behaviour can be different across different OSes and computers.
You've just discovered memory stomping. You can read more about it here: What is a “memory stomp”?
When you allocate
int array[10],i;
, those variables go into memory (specifically, they're allocated on the stack, which is a block of memory associated with the function).array[]
andi
are probably adjacent to each other in memory. It seems that on Windows 8.1,i
is located atarray[10]
. On CentOS,i
is located atarray[11]
. And on Ubuntu, it's in neither spot (maybe it's atarray[-1]
?).Try adding these debugging statements to your code. You should notice that on iteration 10 or 11,
array[i]
points ati
.