strcat causes bus error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bubbles19518
    New Member
    • Dec 2008
    • 5

    strcat causes bus error

    Hey,

    I have this struct
    struct node {
    char *subExpressionT illNow; // the current subexpression
    int isWord; /* To check if this is a valid word to be written into dict.txt,
    0 is false, 1 is true*/
    struct node *paths[26]; // A subexpression can be followed by any of the 26 characters
    };
    and this is the code thats giving me a bus error
    head->paths[letter]->subExpressionT illNow = (char *)malloc(sizeof (head->subExpressionT illNow) + sizeof(char));
    head->paths[letter]->subExpressionT illNow = strcat(head->subExpressionT illNow,&c);
    Its a tree of binary letters, I load them in one character at a time. If the word is "STEVE" You create a node S which subExpressionTi llNow has value of "S". Then you create a node T which has a subexpressionti llnow of "ST".

    Not working right, no clue whats wrong. Any help is greatly appreciated.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Originally posted by bubbles19518
    Code:
    struct node {
    char *subExpressionTillNow;
    ...
    };
    
    head->paths[letter]->subExpressionTillNow =
            (char *)malloc(sizeof(head->subExpressionTillNow) + sizeof(char));
    sizeof(head->subExpressionT illNow) gives you the size of a char pointer. What you want is the length of the string being pointed at.

    What should you do if head->subExpressionT illNow is NULL?
    Make sure you include space for a string-terminating null character.

    sizeof(char) is "1" by definition. You can choose to add the expression or the constant. It won't make any difference.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by bubbles19518
      Code:
      head->paths[letter]->subExpressionTillNow = strcat(head->subExpressionTillNow,&c);
      I am guessing that c is of type char in this statement. This will not work and is likely to produce undefined behaviour because a string is a NULL terminated string of characters. The size of a string with 1 character in it is 2. If you want to do this you need to create a string containing c to use in strcat not just take the address of c.

      Comment

      • bubbles19518
        New Member
        • Dec 2008
        • 5

        #4
        You were right. c was type char. I attempted to create a string and still get a bus error. I also upped malloc to 2 instead of sizeof(char).

        Code:
        	char cc[2];
        	cc[1] = '\0';
        cc[0] = c;
        							head->paths[letter] = (node *)malloc(sizeof(node));
        							head->paths[letter]->subExpressionTillNow = (char *)malloc(sizeof(head->subExpressionTillNow) + 2);
        							head->paths[letter]->subExpressionTillNow = strcat(head->subExpressionTillNow,cc);
        							head->paths[letter]->isWord = 0;
        							head = head->paths[letter];

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          It's been a while since I have coded C but I think for strcat you need to start out with a C string. Like, one of 0 characters:

          Code:
          char* str = malloc(sizeof(char));
          Then intialize the string to a null string:
          Code:
          str[0] = '\0';
          And then just use strcat:
          Code:
          strcat(str, "weaknessforcats");
          You should need to do addtional malloc's.

          However, for strcpy you need to allocate memory for the copy:
          Code:
          char* newstr = malloc((strlen(str) +1) * sizeof(char))
          strcpy(newsrtr, str);

          Comment

          • bubbles19518
            New Member
            • Dec 2008
            • 5

            #6
            Thanks a lot! That did the trick! I ended up just strcpy()ing it instead of strcat()ing.

            Comment

            Working...