Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seg Fault when initializing array

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;

}
like image 732
Raven Dreamer Avatar asked Sep 28 '10 17:09

Raven Dreamer


People also ask

How to fix segmentation fault error?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

Why I am getting segmentation fault?

In practice, segfaults are almost always due to trying to read or write a non-existent array element, not properly defining a pointer before using it, or (in C programs) accidentally using a variable's value as an address (see the scanf example below).

Can free () cause seg fault?

Writing to freed (or out-of-scope) memory might cause a segmentation fault or corrupt data if the memory is recycled and reused. In certain cases, writing malicious data to freed memory can result in arbitrary code execution.

What is segmentation fault in c array?

A space that is not allocated to you but you try to access it anyway. You cannot get in. In other words, it gives an error. The same way if at any point in a program, memory space that is not allocated to a particular variable or code block is accessed a segmentation fault occurs, also called the core dump error.


1 Answers

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);
like image 53
Nikolai Fetissov Avatar answered Nov 09 '22 16:11

Nikolai Fetissov