初始化数组时出现段错误

发布于 2024-09-25 06:23:00 字数 1563 浏览 2 评论 0原文

我正在上 C 课程,并遇到了分段错误。据我了解,当您访问尚未分配的内存或超出边界的内存时,应该会发生段错误。当然,我想做的就是初始化一个数组(虽然相当大),

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

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

完全清楚的是:我正在寻求帮助来理解(并最终修复)我的代码产生段错误的原因。

#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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

遇到 2024-10-02 06:23:00

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

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

顺便说一句,这种比较是向后的:

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

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

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.

戈亓 2024-10-02 06:23:00

您正在尝试抓取堆栈上的 1000 * 1000 * sizeof( int ) 字节。这超过了操作系统允许的堆栈增长。如果在任何 Unix 上 - 检查 ulimit -a进程的最大堆栈大小。

根据经验,使用 malloc(3)。或者使用静态数组 - 在任何函数的范围之外。

在这种情况下,您可以将 MajorArray 的声明替换为:

int (*majorArray)[1000] = calloc(1000, sizeof majorArray);

You are trying to grab 1000 * 1000 * sizeof( int ) bytes on the stack. This is more then your OS allows for the stack growth. If on any Unix - check the ulimit -a for max stack size of the process.

As a rule of thumb - allocate big structures on the heap with malloc(3). Or use static arrays - outside of scope of any function.

In this case, you can replace the declaration of majorArray with:

int (*majorArray)[1000] = calloc(1000, sizeof majorArray);
一瞬间的火花 2024-10-02 06:23:00

我无法在您的代码中找到任何错误,因此我编译并运行它并按预期工作。

但是,您的代码中存在语义错误:

   start=clock();
   //set it up to perform the test 100 times.
   for(int k = 0; k<10; k++)
   {

应该是:

   //set it up to perform the test 100 times.
   for(int k = 0; k<10; k++)
   {
   start=clock();

另外,末尾的条件应更改为其相反:

if(rowMajor<colMajor)

最后,为了避免其他人提到的操作系统特定堆栈大小的问题,您应该定义矩阵在 main() 之外:

#include <stdio.h>
#include <time.h>

int majorArray [1000][1000];

int main(void){
   //first define the array and two doubles to count elapsed seconds.   
   double rowMajor, colMajor;
   rowMajor = colMajor = 0;

I was unable to find any error in your code, so I compiled it and run it and worked as expected.

You have, however, a semantic error in your code:

   start=clock();
   //set it up to perform the test 100 times.
   for(int k = 0; k<10; k++)
   {

Should be:

   //set it up to perform the test 100 times.
   for(int k = 0; k<10; k++)
   {
   start=clock();

Also, the condition at the end should be changed to its inverse:

if(rowMajor<colMajor)

Finally, to avoid the problem of the os-specific stack size others mentioned, you should define your matrix outside main():

#include <stdio.h>
#include <time.h>

int majorArray [1000][1000];

int main(void){
   //first define the array and two doubles to count elapsed seconds.   
   double rowMajor, colMajor;
   rowMajor = colMajor = 0;
锦欢 2024-10-02 06:23:00

这段代码在 Linux 下运行得很好,我看不出有什么明显的错误。你可以尝试通过gdb调试它。像这样编译它:

gcc -g -o testcode test.c

然后

gdb ./testcode

在 gdb 中说 run

如果崩溃了,说 where 然后 gdb 会告诉你崩溃发生的位置。那么现在你就知道错误在哪一行了。

This code runs fine for me under Linux and I can't see anything obviously wrong about it. You can try to debug it via gdb. Compile it like this:

gcc -g -o testcode test.c

and then say

gdb ./testcode

and in gdb say run

If it crashes, say where and gdb tells you, where the crash occurred. Then you now in which line the error is.

∞琼窗梦回ˉ 2024-10-02 06:23:00

该程序在由 gcc 编译时运行良好,&在 Linux 中运行,Cygwin 很可能是您的问题。

The program is working perfectly when compiled by gcc, & run in Linux, Cygwin may very well be your problem here.

城歌 2024-10-02 06:23:00

如果它在其他地方正确运行,您很可能试图获取比操作系统允许的更多的堆栈空间。您在堆栈上分配 4MB(1 mill 整数),这对于在堆栈上“安全”分配来说太多了。 malloc() 和 free() 是您最好的选择。

If it runs correctly elsewhere, you're most likely trying to grab more stack space than the OS allows. You're allocating 4MB on the stack (1 mill integers), which is way too much for allocating "safely" on the stack. malloc() and free() are your best bets here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文