初始化阵列时赛格故障 [英] Seg Fault when initializing array

查看:92
本文介绍了初始化阵列时赛格故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在第C类,并运行到分割故障。据我了解,赛格故障应该当你访问一个尚未分配,或以其他方式范围之外的内存发生。 当然,所有我想要做的是初始化数组(虽然相当大的那个)

我是不是只是误解如何解析二维数组?乱放绑定正是会导致赛格fault--我错了在使用嵌套的 for循环此?

教授提供的时钟功能,所以我希望这不是问题。我运行在Cygwin中该code,会是什么问题?来源$ C ​​$ C如下。使用C99标准为好。

要非常清楚:我在寻找帮助理解(并最终修复)我的code产生赛格故障的原因。

 的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;
诠释主要(无效){
   //首先定义阵列和两个双打记录经历的秒数。
   双rowMajor,colMajor;
   rowMajor = colMajor = 0;
   INT majorArray [1000] [1000] = {};   clock_t表示起点,终点;   //设置它进行测试的100倍。
   为(中间体K = 0; K&小于10; k ++)
   {
   开始=时钟();
   //首先我们做的主要行
   的for(int i = 0; I< 1000;我++)
   {
       对于(INT J = 0; J< 1000; J ++)
       {
           majorArray [I] [J] = 314;
       }
   }
   结束=时钟();
   rowMajor + =(最终开始)/(双)CLOCKS_PER_SEC;
   //在这一点上,我们只是做rowMajor,所以经过= rowMajor
   开始=时钟();
   //现在我们做的主要列
     的for(int i = 0; I< 1000;我++)
   {
       对于(INT J = 0; J< 1000; J ++)
       {
           majorArray [J] [i] = 314;
       }
   }
   结束=时钟();
   colMajor + =(最终开始)/(双)CLOCKS_PER_SEC;
   }
   //现在我们已经做了计算100倍,我们可以比较值。
   的printf(行了重大%F秒\\ n,rowMajor);
   的printf(列了重大%F秒\\ n,colMajor);
   如果(rowMajor< colMajor)
   {
     的printf(行主要是快\\ n);
   }
   其他
   {
      的printf(列重大快\\ n);
   }   返回0;}


解决方案

您程序正常工作在我的电脑(X86-64 / Linux)的,所以我怀疑你正在运行到一个特定的系统限制在通话尺寸堆叠。我不知道你有多堆得到Cygwin的,但你的阵列将达400万个字节(32位 INT ) - 这很容易被过大

尝试移动 majorArray 的声明出来的(把它放在右后的#include S) - 那么这将是一个全局变量,它来自一个不同的分配池,可以更大

顺便说一句,这是比较落后:

 如果(rowMajor> colMajor)
{
  的printf(行主要是快\\ n);
}
其他
{
   的printf(列重大快\\ n);
}

另外,做这样一个测试,你真的应该重复这个过程为许多不同的数组的大小和形状。

I'm taking a class on C, and running into a segmentation fault. From what I understand, seg faults are supposed to occur when you're accessing memory that hasn't been allocated, or otherwise outside the bounds. 'Course all I'm trying to do is initialize an array (though rather large at that)

Am I simply misunderstanding how to parse a 2d array? Misplacing a bound is exactly what would cause a seg fault-- am I wrong in using a nested for-loop for this?

The professor provided the clock functions, so I'm hoping that's not the problem. I'm running this code in Cygwin, could that be the problem? Source code follows. Using c99 standard as well.

To be perfectly clear: I am looking for help understanding (and eventually fixing) the reason my code produces a seg fault.

#include <stdio.h>
#include <time.h>
int main(void){
   //first define the array and two doubles to count elapsed seconds.   
   double rowMajor, colMajor;
   rowMajor = colMajor = 0;
   int majorArray [1000][1000] = {};

   clock_t start, end;

   //set it up to perform the test 100 times.
   for(int k = 0; k<10; k++)
   {
   start=clock();
   //first we do row major
   for(int i = 0; i < 1000; i++)
   {
       for(int j = 0; j<1000; j++)
       {
           majorArray[i][j] = 314;
       }
   }
   end=clock();
   rowMajor+= (end-start)/(double)CLOCKS_PER_SEC;
   //at this point, we've only done rowMajor, so elapsed = rowMajor
   start=clock();
   //now we do column major
     for(int i = 0; i < 1000; i++)
   {
       for(int j = 0; j<1000; j++)
       {
           majorArray[j][i] = 314;
       }
   }
   end=clock();
   colMajor += (end-start)/(double)CLOCKS_PER_SEC;
   }
   //now that we've done the calculations 100 times, we can compare the values.
   printf("Row major took %f seconds\n", rowMajor);
   printf("Column major took %f seconds\n", colMajor);
   if(rowMajor<colMajor)
   {
     printf("Row major is faster\n");
   }
   else
   {
      printf("Column major is faster\n");
   }

   return 0;

}

解决方案

Your program works correctly on my computer (x86-64/Linux) so I suspect you're running into a system-specific limit on the size of the call stack. I don't know how much stack you get on Cygwin, but your array is 4,000,000 bytes (with 32-bit int) - that could easily be too big.

Try moving the declaration of majorArray out of main (put it right after the #includes) -- then it will be a global variable, which comes from a different allocation pool that can be much bigger.

By the way, this comparison is backwards:

if(rowMajor>colMajor)
{
  printf("Row major is faster\n");
}
else
{
   printf("Column major is faster\n");
}

Also, to do a test like this you really ought to repeat the process for many different array sizes and shapes.

这篇关于初始化阵列时赛格故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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