strcat segmentation fault

This is a discussion on strcat segmentation fault within the C Programming forums, part of the General Programming Boards category; hi, i am getting a segmentation fault in the following program. Code: ... FILE *fp; int helpSize=sizeof(char), i; char *helpText; ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    9

    strcat segmentation fault

    hi, i am getting a segmentation fault in the following program.

    Code:
    ...
      FILE *fp;
      int helpSize=sizeof(char), i;
      char *helpText;
      char line[255];
      char *filename="HELP";
      GladeXML *xml;
      GtkWidget *mainTextbox;
      
      if ((fp = fopen(filename, "r")) == NULL) {
        fprintf(stdout, "Unable to open file <%s>\n", filename);
        exit(EXIT_FAILURE);
      } 
      
      helpText = malloc(1);
      i = 1;
      while(fgets(line, sizeof(line), fp) != NULL) {
        printf("%d)", i++); //segmentation fault after the second append
        helpSize += strlen(line) * sizeof(char);
        realloc(helpText, helpSize);
        printf("%d\n", sizeof(helpText)); //size is always 4, what 4 though  and why?
        strcat(helpText, line);
      }
       
      //here goes the statement to display the help
      //xml = glade_get_widget_tree(GTK_WIDGET(button));
      //mainTextbox = glade_xml_get_widget (xml, "main_textbox");
    
      //gtk_text_insert(main_textbox, help, -1, -1);
      //gtk_text_insert(GTK_ENTRY(mainTextbox), NULL, NULL, NULL, "HELP!!", -1);
      //gtk_entry_set_text(mainTextbox, "HELP!!");
     
      free(helpText);
      fclose(fp);
    when i am printf("%s", sixeof(helpText)) after the reallocation of space the answer is always 4. what does that mean? and why i am getting a segmentation fault? any help?

    thanks

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    sorry for that but i dont know howto del this thread. anyway, needed to have a better look to my c reference... traced my error

    Code:
    helpText = realloc(helpText, sizeof(line));

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> and why i am getting a segmentation fault

    you have to capture the return value of realloc because C passes variables by value:

    >> after the reallocation of space the answer is always 4

    when sizeof() is applied to a pointer, it returns the size of a pointer (4, in your case) - not the size of the memory it points to.

    here is how you might recode all that:

    Code:
     char *helpText = NULL; // very important
     // ...
     int size = helpSize + (strlen(line) * sizeof(char));
     char * newblock = realloc(helpText, helpSize);
     if(newblock != NULL) {
      helpText = newblock;
      helpSize = size;
      } else {
      // handle error 
      }
    also, you might look up the documentation on realloc() to see why it works that way...



    ITSA
    Socket Library!

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    thanks sebastiani, your reply has given me even a deeper insight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault on strcat
    By paxmanchris in forum C Programming
    Replies: 9
    Last Post: 04-24-2007, 10:04 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. strcat causing segmentation fault?
    By jbsloan in forum C Programming
    Replies: 2
    Last Post: 04-02-2005, 09:42 AM
  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