Segmentation fault

This is a discussion on Segmentation fault within the C Programming forums, part of the General Programming Boards category; I have an assignment for programming class, to dynamically allocate memory for a matrix. Though as the title indicates, I ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    11

    Segmentation fault

    I have an assignment for programming class, to dynamically allocate memory for a matrix.

    Though as the title indicates, I end up with segfault. Anyone care to have a look?

    At first I have two structs specified in the assignment:

    Code:
    typedef struct{
            char current;
            char next;
    } cell;
    
    typedef struct{
            cell **field;
            int rows;
            int cols;
    } game;
    
    
    int main(...){
    
    game data;
    
    setupGeneration(data);
    
    ....
    
    }
    I call upon the function setupGeneration which opens a file that looks like this for example. I want all lines except for the first to be put into the matrix data.field.current

    3,3
    0x0
    x0x
    0x0

    (The first line sets the size of data.rows and data.cols.)

    Code:
    void **setupGeneration (char **argv, game data){
    
            int i,j, line = 0;
            char c;
    
            FILE *fileOpen;
    
            fileOpen = fopen(argv[1], "r");
    
            data.field = calloc(data.rows,sizeof(char *));
            for(i = 0; i < data.rows; i++){
                    data.field[i] = calloc(data.cols, sizeof(char));
            }
    
    
    
    /* segfault? */
    
            while(c != EOF){
    
                    c = fgetc(fileOpen);
    
                    if(c == '\n'){
                            line++;
                    }
                    if(line >= 1){
                            data.field[i][j].current = c;
    
                            printf("%c",c);
                    }
            }
    
            printf("\n");
    
            return 0;
    }
    As you can see, I've marked the section I believe is causing the segfault.
    Very thankful for any help.

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Any particular reason why you're asking for sizeof(char*) and sizeof(char) instead of sizeof(cell*) and sizeof(cell)?

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    No, not really. Guess it should be cell, huh?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by Miffeltoffel
    Guess it should be cell, huh?
    Better yet:
    Code:
    data.field = calloc(data.rows, sizeof(data.field[0]));
    Code:
    data.field[i] = calloc(data.cols, sizeof(data.field[i][0]));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Welcome to the forum!

    I really really hope not to sound rude, but there is a litany of things that are wrong/don't really make sense to me.

    1) It makes no sense for setupGeneration to return void** if the only code path returns 0. If setupGeneration() doesn't do anything, why not make it void? Otherwise, the function should return something meaningful other than 0.
    2) if you can compile with -g, try gdb ./your_program -> run and then bt when it seg faults. That will print out a stack trace of what was happening, which will tell you exactly where the seg fault is.
    3) This line makes no sense:
    Code:
    data.field = calloc(data.rows,sizeof(char *));
    First off, I see you are trying to calloc data.rows elements of something, but data.rows has not been initialized at all as far as I can tell.
    Second off, why are the elements you are allocating the size of a pointer to char? It seems like you're trying to assign to field, which is of type cell**. So don't you want sizeof(cell*);
    4) Same thing basically:
    Code:
    data.field[i] = calloc(data.cols, sizeof(char));
    Again, data.cols == who knows, while data.field[i] is of type cell*, so it makes no sense to allocate elements the size of a char to it. You'd like the elements to be sizeof(cell).

    I don't know exactly what is causing the seg fault. Although trying 2 can help you, you really need to address 1, 3, and 4. I hope that helps at least a little bit.
    Last edited by QuadraticFighte; 10-21-2010 at 01:30 AM.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    Oh, thank you guys! I've tried to get main to send the adress, but didn't get it to work, the -> did the trick though!


    Now I'm a bit further down the road, but still segfault


    Code:
    void **setupGeneration (char **argv, game *data){
    
            printf("i funktion setupGeneration");
    
            int i,j, line = 0;
            char c;
    
            FILE *fileOpen;
    
            fileOpen = fopen(argv[1], "r");
    
            data->field = calloc(data->rows, sizeof(data->field[0]));
            for(i = 0; i < data->rows; i++){
                    data->field[i] = calloc(data->cols, sizeof(data->field[1][0]));
    
            }
    
    
    
    /* segfault? */
    
            while(c != EOF){
    
                    c = fgetc(fileOpen);
    
                    if(c == '\n'){
                            line++;
                    }
                    if(line >= 1){
    
                      /* THIS IS CAUSING SEGFAULT*/
                            data->field[i][j].current = c;
    
                            printf("%c",c);
                    }
            }
    
            printf("\n");
    
            return 0;
    }
    I've identified the reason to the one marked with bold, and I really doubt what I've done there is the correct way of doing it, but I havn't got a clue how else to do it.

    Thank you guys for your very informative respnses so far, I'm actually getting somewhere.
    Last edited by Miffeltoffel; 10-21-2010 at 01:53 AM. Reason: Bold wasn't as clear as i thought

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by Miffeltoffel View Post
    Oh, thank you guys! I've tried to get main to send the adress, but didn't get it to work, the -> did the trick though!


    Now I'm a bit further down the road, but still segfault


    Code:
    void **setupGeneration (char **argv, game *data){
    
            printf("i funktion setupGeneration");
    
            int i,j, line = 0;
            char c;
    
            FILE *fileOpen;
    
            fileOpen = fopen(argv[1], "r");
    
            data->field = calloc(data->rows, sizeof(data->field[0]));
            for(i = 0; i < data->rows; i++){
                    data->field[i] = calloc(data->cols, sizeof(data->field[1][0]));
    
            }
    
    
    
    /* segfault? */
    
            while(c != EOF){
    
                    c = fgetc(fileOpen);
    
                    if(c == '\n'){
                            line++;
                    }
                    if(line >= 1){
    
                      /* THIS IS CAUSING SEGFAULT*/
                            [I]data->field[j].current = c;
    
                            printf("%c",c);
                    }
            }
    
            printf("\n");
    
            return 0;
    }
    I've identified the reason to the one marked with bold, and I really doubt what I've done there is the correct way of doing it, but I havn't got a clue how else to do it.

    Thank you guys for your very informative respnses so far, I'm actually getting somewhere.
    Sorry about the pass-by-value stuff, it turns out that what you were doing was fine (and this is fine too). I had a brain fart. My apologizes. Are you absolutely sure that line is the problem? Like did you gdb that?

    As a passing glance, would note that j is unintialized, so who knows what's in there? Hence when you go to dereference field you might be out of your address space, which could well be causing your segmentation fault.
    Last edited by QuadraticFighte; 10-21-2010 at 01:56 AM.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    No, turns out you were right (in some of my functions, where i needed to change the original structs values), now that part works.

    another thing I'm struggling with is that I'm trying to set field[i][j] to this section of the file:

    0x00
    000x
    xx00

    Where i is the row number and j is the column.

    So, in other words, I need to skip the first row of the file, and find a way to sort out the \n's.

    I'm thinking strtok() for the latter, but how to accomplish the first?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Have you considered just reading line by line with fgets()?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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