segmentation fault in simple code

This is a discussion on segmentation fault in simple code within the C Programming forums, part of the General Programming Boards category; Hi, I wrote this simple program to time a program. I wanted to time the writes to a matrix. but ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    segmentation fault in simple code

    Hi,
    I wrote this simple program to time a program. I wanted to time the writes to a matrix. but for some reason the program gives me a segmentation fault. can anyone please tell me why.

    Code:
    /* Program to time matrix writes */
    
    #include <stdio.h>
    #include <time.h>
    
    #define SIZE 100
    
    void MatrixWrite(int matrix[SIZE][SIZE]){
            int i = 0;
            int j = 0;
    
            for(i = 0; i < SIZE; i++){
                    for(j = 0; j < SIZE; j++){
                            matrix[i][j] = i+j;  /* initialize matrix[i][j] to i+j */
                            }
                    }
    }
    
    int main()
    {
            clock_t start = clock();
            int matrix[SIZE][SIZE] = {};      /* initialize a matrix to NULL*/
            MatrixWrite(matrix[SIZE][SIZE]);
            printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
            return 0;
    
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Line 8 ... the function should prototype as..
    Code:
    void MatrixWrite(int *Matrix)
      {...
    Line 22 ... the correct initializer is...
    Code:
    int matrix[SIZE][SIZE] = {0};  // initialize to 0
    Line 23 ... the correct call to the function is...
    Code:
    MatrixWrite(matrix);
    Also Line 21... depending on your version of C, you may not be able to place active code before variable declarations. AFIK, that's only supported in C-99.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    I tried the changes you suggested..gave me more warnings

    matrix.c: In function ‘MatrixWrite’:
    matrix.c:14: error: subscripted value is neither array nor pointer
    matrix.c: In function ‘main’:
    matrix.c:22: warning: missing braces around initializer
    matrix.c:22: warning: (near initialization for ‘matrix[0]’)
    matrix.c:23: warning: passing argument 1 of ‘MatrixWrite’ from incompatible pointer type

    Code:
    /* Program to time matrix writes */
    
    #include <stdio.h>
    #include <time.h>
    
    #define SIZE 100
    
    void MatrixWrite(int *matrix){
            int i = 0;
            int j = 0;
    
            for(i = 0; i < SIZE; i++){
                    for(j = 0; j < SIZE; j++){
                            matrix[i][j] = i+j;  /* initialize matrix[i][j] to i+j */
                            }
                    }
    }
    
    int main()
    {
            clock_t start = clock();
            int matrix[SIZE][SIZE] = {0};      /* initialize a matrix to NULL*/
            MatrixWrite(matrix);
            printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
            return 0;
    
    }

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    recanted... see next post
    Last edited by CommonTater; 11-21-2011 at 01:53 AM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, having lead you down he wrong path, here's a working version of your code... with my apologies.

    Code:
    /* Program to time matrix writes */
    
    #include <stdio.h>
    #include <time.h>
    
    #define SIZE 100
    
    void MatrixWrite(int matrix[SIZE][SIZE]){
            int i = 0;
            int j = 0;
    
            for(i = 0; i < SIZE; i++){
                    for(j = 0; j < SIZE; j++){
                            matrix[i][j] = i+j;  /* initialize matrix[i][j] to i+j */
                            }
                    }
    }
    
    int main( void )
    {
            int matrix[SIZE][SIZE] = {0};      /* initialize a matrix to NULL*/
            clock_t start = clock();
            MatrixWrite(matrix);
            printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
            return 0;
    }
    Do note that your elapsed time is almost certainly going to return 0 since this is a very small task that can likely be done in less than a millisecond on most machienes (well, except an IBM XT, maybe)

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    yeah that works. A couple of warnings though. but it compiles and runs..ya I would slowly increase the matrix size and see when it will start showing me some real execution time. Thanks for your help tater.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Hmm actually now theres another problem. I tried to scale the size of matrix to 2000 and now the program crashes with a segmentation fault. I observed (using gdb) that it crashes at the initializing line
    Code:
    int matrix[SIZE][SIZE] = {0};
    What I dont understand is how can a program behavior change with the size of its parameters while the compiler has no problems at all. could this be cos of my system limitations?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You're probably crashing the program's stack... Do the math... 2000 x 2000 x sizeof(int) = 16,000,000 bytes.

    You're going to have to create an array that big on the heap, using malloc() or calloc()...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple shared memory throwing segmentation fault
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 10-03-2010, 06:53 AM
  2. Help with Segmentation fault - in simple code
    By ramchan in forum C Programming
    Replies: 8
    Last Post: 03-01-2009, 08:07 AM
  3. simple server & client getting Segmentation fault error
    By blondieoubre in forum Linux Programming
    Replies: 2
    Last Post: 12-11-2007, 05:26 AM
  4. Replies: 7
    Last Post: 12-10-2004, 12:58 AM
  5. Help with code segmentation fault
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21