Need another set of eyes for debugging...

This is a discussion on Need another set of eyes for debugging... within the C Programming forums, part of the General Programming Boards category; i've been staring at this code for weeks now and i can't figure out why it segfaults. can someone have ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    8

    Question Need another set of eyes for debugging...

    i've been staring at this code for weeks now and i can't figure out why it segfaults. can someone have a look at it and give me some feedback? thanks!

    the function opens a file located in the users ~ directory (that's about as far into linux programming as this code gets BTW). the line from the fileis formatted as follows

    prog sometext morestuff xterm -e ssh blah blah

    it skips the leading "prog" (intentionally) and grabs sometext, puts into an array, then gets morestuff, also puts into an array, then reads the rest into an array as it appears, spaces and all. this works well. however i get a segfault at the end of the function call. here is the functions code...

    Code:
    void initialize(void){
    
      int i = 0, count = 5;
      char hover[75] = { };
      char icon[75] = { };
      char prog[75] = { };
      char path[50] = { };
      char *hold;
    
      const char *file = { "/.icewm/toolbar" }; // default path to toolbar file
    
      struct passwd *name;
      name = getpwuid( getuid() );
    
      sprintf( path, "%s%s", (char *)name->pw_dir, (char *)file);  // create ~/.icewm/toolbar path
    
    
    FILE *fptr;
    char *p;
    fptr = fopen(path, "r" );
    
     hold = (char *)malloc(sizeof(fptr)*sizeof(char) );
    
      fgets(hold, 100, fptr);
    
    do{
       if ( ( p = strchr ( hold, '\n' ) ) != NULL ) *p = '\0';
       printf("\ninitialized - contains: [%s]\n", hold);
    
       count = 5;
    
    
       memset(hover,0,75);  //reset memory to 0
       memset(icon,0,75);
       memset(prog,075);
      i = 0;
    
      // the print statements in the following
      // while loops are for checking memory
      // contents only.
    
      while(hold[count] != ' '){
       printf("[%c] ", hold[count]);
        hover[i] = hold[count];
        ++i;
        ++count;
      }
    
      i = 0;
      ++count;
    
      while(hold[count] != ' '){
        icon[i] = hold[count];
        ++i;
        ++count;
      }
     memset(hold,0,sizeof(hold)); //reset hold to 0
    
    
    } while( (fgets(hold, 100, fptr)) != NULL);
    
     fclose(fptr);
    
     // check the values of the last iteration
     // just to make sure. (this is not a feature)
    printf("\n\nFinal hover: %s\nicon: %s\nprog: %s\n", hover, icon, prog);
    
     memset(hover,0,75);
     memset(icon,0,75);
     memset(prog,0,75);
     memset(hold,0,sizeof(hold));
    
     getchar();
    }
    
    
      i = 0;
      ++count;
    
      while(hold[i] != '\0'){
        prog[i] = hold[count];
        ++i;
        ++count;
      }
    
     i = 0;
    thanks again.

    -jstn

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > hold = (char *)malloc(sizeof(fptr)*sizeof(char) );
    Which gets you 4 bytes
    I dont know what you were trying to do with the malloc. Even if you were attempting to get the file size, it would still be wrong

    > fgets(hold, 100, fptr);
    Here's where you try and cram 100 bytes into your 4 byte buffer - the fuze to your segfault has just been lit

    Forget the malloc, and just go with
    char hold[100];

    > sprintf( path, "%s%s", (char *)name->pw_dir, (char *)file);
    It's doubtful that you need these casts


    > fptr = fopen(path, "r" );
    Should be followed by
    Code:
    if ( fptr == NULL ) {
      perror( "can't read" );
      exit(1);
    }
    When you've got that working, you should be able to get rid of those memset calls, by careful placement of a couple of
    hover[i] = '\0';
    statements

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    8
    ok i'll make the additions to the file after lunch. BTW i check for the existance of the file before this function is called. i know i'm at LEAST correct there thanks for the input! i will post back in a few.

    -justin

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    8
    Originally posted by Salem
    [B]> hold = (char *)malloc(sizeof(fptr)*sizeof(char) );
    Which gets you 4 bytes
    I dont know what you were trying to do with the malloc. Even if you were attempting to get the file size, it would still be wrong
    hold is what holds each line as it is read from the file.

    > fptr = fopen(path, "r" );
    Should be followed by
    [code]
    if ( fptr == NULL ) {
    perror( "can't read" );
    exit(1);
    }
    the file checking is done in a seperate function call ealier in the program, so i'm sure that the file exists or i wouldn't be at this point in the program.

    i'll make the additions to my program and post back. thanks for the input!

    -justin

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    8
    well here i am 5 minutes later, happy to report that i just removed the hold = (char *)malloc(sizeof(fptr)*sizeof(char) ); and instead used char hold[250] = { }; which worked like a charm!

    thanks so much Salem. i appreciate the insight, however i know, in the future, i will post again!

    -justin

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Speaking of linux, do you need special compilers for compiling code for linux programming or do you just need the right functions and code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SystemParametersInfo set wallpaper issues
    By A10 in forum Windows Programming
    Replies: 5
    Last Post: 03-14-2008, 07:39 PM
  2. Replies: 8
    Last Post: 01-18-2008, 03:06 AM
  3. Replies: 4
    Last Post: 01-13-2008, 01:14 AM
  4. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  5. Set default directory with GetTempDir?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2003, 11:36 PM

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