Seg Fault when initializing array

后端 未结 6 566
忘了有多久
忘了有多久 2020-12-11 03:57

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 all

相关标签:
6条回答
  • 2020-12-11 04:27

    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;
    
    0 讨论(0)
  • 2020-12-11 04:28

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

    0 讨论(0)
  • 2020-12-11 04:30

    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);
    
    0 讨论(0)
  • 2020-12-11 04:34

    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.

    0 讨论(0)
  • 2020-12-11 04:38

    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.

    0 讨论(0)
  • 2020-12-11 04:45

    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.

    0 讨论(0)
提交回复
热议问题