我最近开始学习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;
}

在我运行Ubuntu 14.04的笔记本电脑上,这段代码没有崩溃。它运行到完成。在我学校运行CentOS 6.6的电脑上,它也运行得很好。在Windows 8.1上,循环永远不会终止。

更奇怪的是,当我将for循环的条件编辑为:I <= 11时,代码只在运行Ubuntu的笔记本电脑上终止。它永远不会在CentOS和Windows中终止。

有人能解释一下内存中发生了什么吗?为什么运行相同代码的不同操作系统会产生不同的结果?

编辑:我知道for循环越界了。我是故意这么做的。我只是不明白在不同的操作系统和计算机上,这种行为是如何不同的。


当前回答

当你遍历i==9时,你把0赋给了实际上位于数组后面的“数组项”,所以你覆盖了一些其他数据。最可能的情况是,你重写了i变量,它位于a[]之后。这样,您只需将i变量重置为零,从而重新启动循环。

如果你在循环中输出i,你自己就会发现:

      printf("test i=%d\n", i);

而不仅仅是

      printf("test \n");

当然,这个结果很大程度上取决于变量的内存分配,而这又取决于编译器及其设置,所以它通常是Undefined Behavior——这就是为什么在不同的机器或不同的操作系统或不同的编译器上的结果可能不同。

其他回答

因为你创建了一个大小为10的数组,for循环条件应该如下所示:

int array[10],i;

for (i = 0; i <10 ; i++)
{

目前,您正在尝试使用数组[10]从内存中访问未分配的位置,这导致了未定义的行为。未定义行为意味着您的程序将以不确定的方式运行,因此它可以在每次执行时给出不同的输出。

当你遍历i==9时,你把0赋给了实际上位于数组后面的“数组项”,所以你覆盖了一些其他数据。最可能的情况是,你重写了i变量,它位于a[]之后。这样,您只需将i变量重置为零,从而重新启动循环。

如果你在循环中输出i,你自己就会发现:

      printf("test i=%d\n", i);

而不仅仅是

      printf("test \n");

当然,这个结果很大程度上取决于变量的内存分配,而这又取决于编译器及其设置,所以它通常是Undefined Behavior——这就是为什么在不同的机器或不同的操作系统或不同的编译器上的结果可能不同。

我将建议一些我在上面没有发现的东西:

赋值数组[i] = 20;

我想这应该会终止所有的代码..(如果你保持i< =10或ll)

如果运行此程序,您可以确定这里指定的答案已经是正确的[与内存踩脚有关的答案为ex。]

在我运行Ubuntu 14.04的笔记本电脑上,这段代码不会中断它的运行直至完成。在我学校运行CentOS 6.6的电脑上,它也运行得很好。在Windows 8.1上,循环永远不会终止。 更奇怪的是,当我将for循环的条件编辑为:I <= 11时,代码只在运行Ubuntu的笔记本电脑上终止。CentOS和Windows永远不会终止。

您刚刚发现了内存踩踏。你可以在这里读到更多关于它的信息:什么是“memory stomp”?

当你分配int数组[10],i;,这些变量进入内存(具体地说,它们分配在堆栈上,这是一个与函数相关的内存块)。数组[]和I在内存中可能是相邻的。似乎在Windows 8.1中,i位于数组[10]中。在CentOS上,i位于数组[11]中。在Ubuntu上,它不在这两个位置(也许它在数组[-1]?)

尝试将这些调试语句添加到代码中。你应该注意到在迭代10或11时,数组[i]指向i。

#include <stdio.h>
 
int main() 
{ 
  int array[10],i; 
 
  printf ("array: %p, &i: %p\n", array, &i); 
  printf ("i is offset %d from array\n", &i - array);

  for (i = 0; i <=11 ; i++) 
  { 
    printf ("%d: Writing 0 to address %p\n", i, &array[i]); 
    array[i]=0; /*code should never terminate*/ 
  } 
  return 0; 
} 

Well, C compiler traditionally does not check for bounds. You can get a segmentation fault in case you refer to a location that does not "belong" to your process. However, the local variables are allocated on stack and depending on the way the memory is allocated, the area just beyond the array (array[10]) may belong to the process' memory segment. Thus, no segmentation fault trap is thrown and that is what you seem to experience. As others have pointed out, this is undefined behavior in C and your code may be considered erratic. Since you are learning C, you are better off getting into the habit of checking for bounds in your code.