Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   A weird segmentation fault error (http://www.go4expert.com/forums/weird-segmentation-fault-error-t27400/)

uwowizard 19Dec2011 06:41

A weird segmentation fault error
 
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.

xpi0t0s 20Dec2011 21:28

Re: A weird segmentation fault error
 
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.


All times are GMT +5.5. The time now is 00:53.