does strcat reallocs ?

This is a discussion on does strcat reallocs ? within the C Programming forums, part of the General Programming Boards category; I'm experiencing something strange here: In the code below I malloced filename to the strlen of the category name, + ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    24

    does strcat reallocs ?

    I'm experiencing something strange here:
    In the code below I malloced filename to the strlen of the category name, + 4 to add ".dat". When I do that I get stuffing in my string.

    Now my question: When I malloc filename to 1 (just to test) the filename string gets filled correctly. This is kinda strange to me, does strcat reallocs or something ?

    Code:
    int get_questions(char*** stringarray, char* category)
    {
        char* filename = NULL;
        int i = 0;
        
        filename = malloc(strlen(category)+4);
        //filename = malloc(1);
        getfilename("rubrieken/", category, filename);
        
        printf("%s", filename);
        
        return i;
    }
    
    void getfilename(char* dir, char* string, char* filename)
    {	
        strcat(filename, dir);
        strcat(filename, string);
        strcat(filename, ".dat");
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    no, strcat() does not change the length of the string. you have to do that yourself.

    >> strcat(filename, dir);
    that will crash because strcat looks for the first null terminator in the string then appends the new string at that point. Your string is unitialized, so strcat() will probably start appending way beyond the allocated memory.

    you can fix that problem by setting the first byte of the string to 0 before doing the first strcat();
    Last edited by Ancient Dragon; 03-26-2006 at 11:35 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > When I malloc filename to 1 (just to test) the filename string gets filled correctly
    That just means you got lucky.
    The problems would start to appear on the next call to malloc or free, when the memory you overwrote would be used for it's real purpose.

    > filename = malloc(strlen(category)+4);
    In fact you need to count the \0 as well, so its
    filename = malloc(strlen(category)+4+1);

    > strcat(filename, dir);
    malloc does not guarantee that the memory will contain any particular value, so you should begin with a strcpy(), not a strcat()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. argv change
    By dracayr in forum C Programming
    Replies: 9
    Last Post: 04-10-2009, 02:49 AM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 02:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-13-2001, 11:09 PM

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