free() problem

Discussion in 'C Programming' started by James Leddy, Jun 26, 2003.

  1. James Leddy

    James Leddy Guest

    Hello,

    I'd like to take a string passed as an argument and realloc() the string to
    have more spaces. Then I thought that I would probably have to free() the
    string. But now I am confused

    If I free() the string in the called function what happens to the data that
    that was passed by the calling function.

    Ok, so what if I free()d it by the calling function. But then again I
    didn't pass the string as a double pointer, so I don't know really how that
    would work. And what if I passed a string literal in, how would the
    calling function be responsible for that?

    Here is the specific instance. As many of you may know, blowfish algorythim
    bepends on a key, which I have chosen to represent as a string. I'm
    supposed to wrap the string if I run out of chars, but since I need it in 4
    char blocks, I decided to add three chars to the end

    int InitializeBlowfish(char *key, size_t len)
    {...

    key = realloc(key, len + 4); //give 3 more spaces,
    memcpy(key + len, key, sizeof(char) * 3);
    ....}

    How do I go about free()ing the memory I realloc()ed?
     
    James Leddy, Jun 26, 2003
    #1
    1. Advertisements

  2. If you want to preserve the contents of the memory pointed to by the
    pointer passed to your function, you can allocate the needed space, copy
    the contents of the original memory to that space, and free it at the end
    of your function.
    Are you trying to get free the extra 3 characters worth of space you
    allocated? Just to a realloc with the original size.

    Also, your use of realloc above is buggy. You do not check if realloc
    succeeded, and you do not preserve the value of key to deal with the case
    realloc fails. Finally, sizeof(char) is 1 by definition, no need to
    clutter code.

    You might find:

    http://www.dinkumware.com/manuals/reader.aspx?b=c/&h=stdlib.html#realloc

    useful.

    Sinan.
     
    A. Sinan Unur, Jun 26, 2003
    #2
    1. Advertisements

  3. James Leddy

    bd Guest

    If you want to resize it, there's no need to free - realloc takes care of
    everything except updating all pointers to it.
    It's gone, then.
    You must make provisions to pass back the updated pointers if you realloc
    in a called function.
    You don't - by doing that you invalidated the pointer passed in. Try:
    int InitializeBlowfish(const char *key, size_t len){
    char *newkey;
    size_t newlen = len;
    if(newlen % 4)
    newlen += 4 - (newlen % 4);
    newkey = malloc(newlen);
    memcpy(newkey, key, len);
    memcpy(newkey + len, key, newlen - len);
    /* ... */
    free(newkey);
    }

    Also, sizeof char is 1 by definition.
     
    bd, Jun 26, 2003
    #3
  4. James Leddy

    bd Guest

    But the old pointer may be invalid now. realloc can move the data to a new
    location if necessary.
     
    bd, Jun 26, 2003
    #4
  5. You are right, of course. I should have pointed that out as well as
    commenting on the OP's usage of realloc.

    Sinan.
     
    A. Sinan Unur, Jun 26, 2003
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.