初始化数组时出现段错误 [英] Seg Fault when initializing array

查看:38
本文介绍了初始化数组时出现段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 C 语言,但遇到了分段错误.据我了解,当您访问尚未分配的内存或超出范围的内存时,应该会发生段错误.'当然我想要做的就是初始化一个数组(虽然相当大)

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)

我只是误解了如何解析二维数组吗?错误放置一个边界正是会导致段错误的原因——我为此使用嵌套的 for-loop 有错吗?

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?

教授提供了时钟功能,所以我希望这不是问题.我在 Cygwin 中运行此代码,这可能是问题吗?源代码如下.也使用 c99 标准.

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
", rowMajor);
   printf("Column major took %f seconds
", colMajor);
   if(rowMajor<colMajor)
   {
     printf("Row major is faster
");
   }
   else
   {
      printf("Column major is faster
");
   }

   return 0;

}

推荐答案

您的程序在我的计算机 (x86-64/Linux) 上正常运行,所以我怀疑您遇到了系统特定的调用大小限制堆.我不知道您在 Cygwin 上获得了多少堆栈,但您的数组是 4,000,000 字节(使用 32 位 int) - 这很容易变得太大.

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.

尝试将 majorArray 的声明从 main 中移出(放在 #includes 之后)——那么它将是一个全局变量,它来自可能更大的不同分配池.

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
");
}
else
{
   printf("Column major is faster
");
}

此外,要进行这样的测试,您确实应该针对许多不同的阵列大小和形状重复该过程.

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

这篇关于初始化数组时出现段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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