Thread: A weird segmentation fault error with 2D array

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    A weird segmentation fault error with 2D array

    Hi there,

    I'm experiencing a weird segmentation fault. 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;
    	    
        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;
    }
    Could anybody tell me what's the issue here? Any help would be greatly appreciated..

    Thank you in advance.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by uwowizard View Post
    Code:
        double b[matrixSize][matrixSize];
    Code:
        static double b[matrixSize][matrixSize];
    Try adding static; to make the second one from the heap instead of stack. You are likely crashing the stack.



    Tim S.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Both those arrays are being declared in the stack - which is a small segment of memory.

    For larger arrays/more arrays, malloc or calloc is your goto function for array allocation. LOTS of memory there.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You are probably running out of stack space. You either need to use dynamic allocation (malloc/free) or create a static variable so the heap is used instead.

    Jim

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by uwowizard View Post
    Hi there,

    I'm experiencing a weird segmentation fault. The following code runs fine
    Could anybody tell me what's the issue here? Any help would be greatly appreciated..

    Thank you in advance.
    Program stack = ~1 - 2 mb on most systems

    1000 x 1000 x sizeof(double) = 8 mb

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is Segmentation fault and why do we get this error?
    By sumit180288 in forum C Programming
    Replies: 2
    Last Post: 09-12-2011, 12:05 AM
  2. Segmentation Fault Error
    By ruanhupo in forum C Programming
    Replies: 2
    Last Post: 04-19-2011, 01:57 AM
  3. Segmentation Fault Error
    By c3jcarmy in forum C Programming
    Replies: 4
    Last Post: 01-25-2011, 09:48 AM
  4. Segmentation fault Error
    By unknown_ in forum C Programming
    Replies: 7
    Last Post: 03-21-2010, 01:32 PM
  5. Segmentation fault error
    By ashok449 in forum C Programming
    Replies: 31
    Last Post: 03-02-2008, 02:13 AM