Why does this for loop exit on some platforms and not on others?

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.

14 Answers
14

Leave a Comment