A weird segmentation fault error

uwowizard's Avatar, Join Date: Dec 2011
Newbie 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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
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.