#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Segmentation fault problem


    hi i want to make a parser that reads from a file and saves thetokens into a struct i am using this code but i keep getting segmentation fault

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <strings.h>
    
    typedef struct{
      int label;
      int id;
      int r1;
      int r2;
      int r3;
    }instruction;
    
    int main(int argc,char *argv[])
    {
        FILE *input;
        char str[1000];   
        char delim[] = " ";
        char *result;
    
     input = fopen(argv[1], "r"); // error check this!
     int i=0;
        instruction *instr = (instruction*)(malloc(100*sizeof(instruction)));
        while  (fgets(str,1000,input)!=NULL){
      
            result = strtok(str, delim);
            instr[i].label = atoi(result);
    
            result = strtok(NULL, delim);
            instr[i].id = atoi(result);
     
    	result = strtok(str, delim);
            instr[i].r1 = atoi(result);  
             
    	result = strtok(NULL, delim);        
            instr[i].r2 = atoi(result); 
    
    	result = strtok(str,delim);
            instr[i].r3 = atoi(result); 
     
           i++;
        }
        fclose(input);
        return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    All your strtok() calls except the first, should have NULL for their first argument.
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Also, at each step, you should have
    if ( result != NULL )
    before passing result onto atoi()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    tried your advice but the same error appears =/
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    By chance when you invoke your program do you pass it a file name argument? If you don't your argv[1] is NULL.

    Code:
    $ supreme_parser filename.goes.here.my_funky_language
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Don't just post "I tried and it didn't work". You need to post actual code changes and actual test commands and results.

    > input = fopen(argv[1], "r"); // error check this!
    So DO IT!
    - check argv[1] actually exists as a command line argument.
    - check that the file was opened successfully.

    > instruction *instr = (instruction*)(malloc(100*sizeof(instruction)));
    CHECK IT!
    While you're at it
    - remove the cast, so it's instruction *instr = malloc(100*sizeof(instruction));
    - add #include <stdlib.h>

    If you get some error message about "cannot convert void*", then change your filename / build process so it compiles C code and not C++ code.

    Casting malloc in C is as best a waste of time, and at worst a dangerous bug.
    At it's most dangerous, the cast masks your failure to include stdlib.h (as you have done), at which point the compiler assumes extern int malloc();
    But this is a lie (it returns a pointer). On machines where pointers are larger than ints (which is getting fairly common at the moment with 64-bit machines becoming widely available), the implicit int and cast will lose information, giving you a garbage pointer for your trouble (and handing you a segfault for your reward).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo