为什么这对于在一些平台上,而不是别人循环退出? [英] Why does this for loop exit on some platforms and not on others?

查看:103
本文介绍了为什么这对于在一些平台上,而不是别人循环退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习C,我采取了类C作为主题。我目前正在玩弄循环和我遇到一些奇怪的行为,我不知道该怎么解释。

 的#include<&stdio.h中GT;诠释的main()
{
  int数组[10],I;  对于(i = 0; I< = 10;我++)
  {
    数组[我] = 0; / * code不应该终止* /
    的printf(测试\\ n);  }
  的printf(%d个\\ N的sizeof(阵列)/的sizeof(INT));
  返回0;
}

在我的笔记本电脑运行Ubuntu 14.04,这code不破。它运行完成。在运行CentOS的6.6我的学校的计算机,它也运行良好。在Windows 8.1,循环永远不会终止。

什么是更奇怪的是,当我修改循环的状态: I< = 11 中,code只能终止我的笔记本电脑运行Ubuntu。它永远不会终止的CentOS和Windows。

任何人能解释发生的事情在内存中,为什么不同的操作系统上运行同一code给出不同的结果?

编辑:我知道for循环超出范围。我故意这样做。我只是想不通的行为如何能在不同的操作系统和计算机的不同。


解决方案

  

在我的笔记本电脑运行Ubuntu 14.04,这code不打破它运行结束。在运行CentOS的6.6我的学校的计算机,它也运行良好。在Windows 8.1,循环永远不会终止。


  
  

什么是更奇怪的是,当我编辑的条件的循环为: I< = 11 中,code只能终止我的笔记本电脑运行Ubuntu。 CentOS的和Windows永远不会终止。


您刚刚发现内存跺脚。你可以阅读更多关于它在这里:什么是记忆跺脚

当你分配 int数组[10],I; ,这些变量存储在内存中(具体地说,他们就分配到堆栈,这是一个内存块上与功能相关联)。 []数组 I 很可能是在内存中彼此相邻。如此看来,在Windows 8.1, I 位于数组[10] 。在CentOS, I 位于数组[11] 。而在Ubuntu上,它在没有现货(也许是在数组[-1] ?)。

尝试添加这些调试语句您code。你应该注意到迭代的10或11,数组[我]在 点我

 的#include<&stdio.h中GT;诠释的main()
{
  int数组[10],I;  的printf(数组:%P,放大器; I:%P \\ N,数组和放大器;我);
  的printf(我是偏离阵列\\ n%D,&安培;我 - 数组);  对于(i = 0; I< = 11;我++)
  {
    的printf(%d个:写0,以解决%P \\ N,我,和放大器;阵列[我]);
    数组[我] = 0; / * code不应该终止* /
  }
  返回0;
}

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.

解决方案

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 is more strange is when I edit the conditional of the for loop to: i <= 11, the code only terminates on my laptop running Ubuntu. CentOS and Windows never terminates.

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[] and i are probably adjacent to each other in memory. It seems that on Windows 8.1, i is located at array[10]. On CentOS, i is located at array[11]. And on Ubuntu, it's in neither spot (maybe it's at array[-1]?).

Try adding these debugging statements to your code. You should notice that on iteration 10 or 11, array[i] points at 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; 
} 

这篇关于为什么这对于在一些平台上,而不是别人循环退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆