1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

A weird segmentation fault error

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

  1. uwowizard

    uwowizard New Member

    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

    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