Thread: Segmentation Fault when dynamically allocating 2d array

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    Segmentation Fault when dynamically allocating 2d array

    This is a homework assignment where I have to read a file into a dynamically allocated 2d array. The file format is
    10
    Jim 3.6
    Jake 4.0
    Will 3.0
    Sara 3.4
    Mike 2.5
    Ellen 2.9
    Melissa 3.9
    Eric 3.8
    John 3.5
    Beth 3.9

    where 10 is the number of students followed by the students and the gpa's. There is more to the program but I have not implemented it yet because I am getting a segmentation fault. The output I am getting when I print the array is
    Jim 3.6
    Jake 4.0
    Will 3.0
    Sara 3.4
    Segmentation fault

    I can see where the problem lies. If I raise value for row when I am allocating the rows of the array, all of the names print. I just do not see why I need to. From my understanding the row * sizeof(char*) should give me enough room for 10 entries. Its probably something I am going to smack myself in the head for but any help is appreciated. Thanks in advance.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void sort();
    
    int main()
    {
        sort();
        return 0;
    }
    
    void sort()
    {
        FILE *fp;
        fp = fopen("data.txt","r");
        int col = 26;
        int row;
        fscanf(fp, "%d",&row);    /*get # of students*/
        char** buffer = (char **)malloc(row * sizeof(char*));
        int i;
        for(i=0; i < row; i++)
        {
            buffer[i] = (char *)malloc(col * sizeof(char));
        }
        /*char buffer[row][col];*/
        for(i=0; i<row; i++)
        {
            fgets(buffer[i], sizeof buffer[i], fp);
        }
        
        for ( i = 1 ; i < row ; i++ ) /*print array*/
        {
            printf ( "%s", buffer[i] );
        }
        for(i = 0; i < col; i++){  /*free array*/
            free(buffer[i]);  
        }      
        free(buffer);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following snippet:
    Code:
        int col = 26;
        int row;
        for(i=0; i < row; i++)
        {
            buffer[i] = malloc(col * sizeof(char));
        }
    ...
        for(i = 0; i < col; i++){  /*free array*/
            free(buffer[i]);
        }
    Do you notice any differences in these loops? What happens if row is smaller than col?

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    I figured out the segmentation fault was when I freed the 2d array. The problem now is it is only printing half of the names.
    Jim 3.6
    Jake 4.0
    Will 3.0
    Sara 3.4
    Mike 2.
    This is the output I get now. It seems if I have the array print specific rows, it is skipping rows 0, 2, 4.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    For element zero your loop is starting at 1 not zero, arrays in C/C++ start at zero and stop at size -1.

    Jim

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Sorry I forgot to say I fixed that i=1. The output is still the same as previously posted.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    For the missing first item the cause is this line:
    Code:
        fscanf(fp, "%d",&row);    /*get # of students*/
    That line leaves the end of line character in the input buffer, you need to manually remove this character before using fgets().

    For the next problem, I suggest you print the result of sizeof( buffer[i]) in your read loop. And maybe print a new line in your print loop.

    Jim

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Ok thanks Jim. That explains alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating 4 MB of Memory causes Segmentation Fault
    By TangoOversway in forum C++ Programming
    Replies: 15
    Last Post: 03-15-2013, 11:29 AM
  2. dynamically allocating an array
    By Lina_inverse in forum C Programming
    Replies: 6
    Last Post: 10-07-2012, 06:06 AM
  3. dynamically allocating a 2d array
    By ali.franco95 in forum C Programming
    Replies: 3
    Last Post: 10-18-2011, 09:28 AM
  4. Dynamically allocating 3D array
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 02-26-2008, 04:07 PM
  5. Dynamically Allocating a 2D Array.
    By LightsOut06 in forum C Programming
    Replies: 17
    Last Post: 10-09-2005, 12:55 AM