我最近开始学习C,我正在以C为主题.我正在玩循环,我遇到了一些我不知道如何解释的奇怪行为.
#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;
}
Run Code Online (Sandbox Code Playgroud)
在我的运行Ubuntu 14.04的笔记本电脑上,此代码不会中断.它运行完成.在我学校的运行CentOS 6.6的计算机上,它运行良好.在Windows 8.1上,循环永远不会终止.
更奇怪的是,当我将for
循环条件编辑为:时i <= 11
,代码只会在运行Ubuntu的笔记本电脑上终止.它永远不会在CentOS和Windows中终止.
任何人都可以解释内存中发生的事情以及运行相同代码的不同操作系统为什么会产生不同的结果?
编辑:我知道for循环超出范围.我是故意这样做的.我无法弄清楚不同操作系统和计算机之间的行为有何不同.