1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A weird segmentation fault error

Discussion in 'C' started by uwowizard, Dec 19, 2011.

  1. uwowizard

    uwowizard New Member

    Joined:
    Dec 19, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi there,

    There is a weird segmentation fault error. The following code runs fine

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    main()
    {
        int matrixSize = 1000;
        int i,j;
        
        double a[matrixSize][matrixSize];
        for (i = 0; i < matrixSize; i++)
            for (j = 0; j < matrixSize; j++)
                a[i][j] = rand() % 10;
    	    
        double b[matrixSize][matrixSize];
        for (i = 0; i < matrixSize; i++)
            for (j = 0; j < matrixSize; j++)
                b[i][j] = rand() % 10;
    
        return 0;
    }
    
    
    But when I try to initialize one more 2D array, I get "segmentation fault" exception:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    main()
    {
        int matrixSize = 1000;
        int i,j;
        
        double a[matrixSize][matrixSize];
        for (i = 0; i < matrixSize; i++)
            for (j = 0; j < matrixSize; j++)
                a[i][j] = rand() % 10;
    	    
        double b[matrixSize][matrixSize];
        for (i = 0; i < matrixSize; i++)
            for (j = 0; j < matrixSize; j++)
                b[i][j] = rand() % 10;
    
        return 0;
    }
    
    
    Any advice on what can be a potential cause would be greatly appreciated..

    Thank you in advance.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    I can't see any difference between the two code samples.

    However I think I might have an idea. How much stack space do you have? In particular, do you have enough for 1000*1000*sizeof(double)*2 bytes? That's 8MB if sizeof(double) is 4.
     

Share This Page