views:

609

answers:

6

So I have a couple of functions that work with a string type I have created. One of them creates a dynamically allocated sting. The other one takes said string, and extends it. And the last one frees the string. Note: The function names are changed, but all are custom-defined by me.

string new = make("Hello, ");
adds(new, "everyone");
free(new);

The code above works - it compiles and runs fine. The code below does not work - it compiles, runs, and then

string new = make("Hello, ");
adds(new, "everyone!");
free(new);

The difference between the code is that the adds() function is adding 1 more character (a !). The character it adds makes no difference - just the length. Just for completeness, the following code does not work:

string new = make("Hello, ");
adds(new, "everyone");
adds(new, "!");
free(new);

Oddly, the following code, which uses a different function, addc() (which adds 1 character instead of a string) works:

string new = make("Hello, ");
adds(new, "everyone");
addc(new, '!');
free(new);

The following, which also does the same thing, works:

string new = make("Hello, everyone!");
free(new);

The error that all the ones that don't work give is this:

test(526) malloc: *** error for object 0x100130: double free
*** set a breakpoint in malloc_error_break to debug

(test is the extremely descriptive name of the program I have this in.)

As far as the function internals, my make() is a call to strlen() and two calls to malloc() and a call to memcpy(), my adds() is a call to strlen(), a call to realloc(), and a call to memcpy(), and my free() is two calls to the standard library free().

So are there any ideas why I'm getting this, or do I need to break down and use a debugger? I'm only getting it with adds()es of over a certain length, and not with addc()s.

Breaking down and posting code for the functions:

typedef struct _str {
  int _len;
  char *_str;
} *string;

string make(char *c)
{
    string s = malloc(sizeof(string));
    if(s == NULL) return NULL;
    s->_len = strlen(c);
    s->_str = malloc(s->_len + 1);
    if(s->_str == NULL) 
      {     
        free(s);
        return NULL;
      }
    memcpy(s->_str, c, s->_len);
    return s;
}

int adds(string s, char *c)
{
    int l = strlen(c);
    char *tmp;
    if(l <= 0) return -1;
    tmp = realloc(s->_str, s->_len + l + 1);
    if(!tmp) return 0;
    memcpy(s->_str + s->_len, c, l);
    s->_len += l;
    s->_str[s->_len] = 0;
    return s->_len;
}

void myfree(string s)
{
    if(s->_str) free(s->_str);
    free(s);
    s = NULL;
    return;
}
A: 

Probably should post the code, but the double free means you are calling free on the same pointer twice.

  1. Are you adding 1 to strlen for the \0 byte at the end?
  2. Once you free a pointer, are you setting your member variable to NULL so that you don't free again (or to a known bad pointer like 0xFFFFFFFF)
Lou Franco
Yes on both. I probably should post the code, but I'd probably try more extensive testing before I resort to making other people look for my mistakes.
Chris Lutz
A: 

Why does "my free() is two calls to the standard library free()." Why are you calling free twice? You should only need to call once.

Please post your adds(); and free() functions.

Mitch Wheat
The struct has a pointer in it, and the make() function malloc()s the struct AND the pointer inside the struct. The free() function, therefore, free()s the pointer inside the struct as well as the struct itself. Posted code.
Chris Lutz
+4  A: 

The first malloc in make should be:

malloc (sizeof (struct _str));

Otherwise you're only allocating enough space for a pointer to struct _str.

dreamlax
+4  A: 

A number of potential problems I would fix:

1/ Your make() is dangerous since it's not copying across the null-terminator for the string.

2/ It also makes little sense to set s to NULL in myfree() since it's a passed parameter and will have no effect on the actual parameter passed in.

3/ I'm not sure why you return -1 from adds() if the added string length is 0 or less. First, it can't be negative. Second, it seems quite plausible that you could add an empty string, which should result in not changing the string and returning the current string length. I would only return a length of -1 if it failed (i.e. realloc() didn't work) and make sure the old string is preserved if that happens.

4/ You're not storing the tmp variable into s->_str even though it can change - it rarely re-allocates memory in-place if you're increasing the size although it is possible if the increase is small enough to fit within any extra space allocated by malloc(). Reduction of size would almost certainly re-allocate in-place unless your implementation of malloc() uses different buffer pools for different-sized memory blocks. But that's just an aside, since you're not ever reducing the memory usage with this code.

5/ I think your specific problem here is that you're only allocating space for string which is a pointer to the structure, not the structure itself. This means when you put the string in, you're corrupting the memory arena.

This is the code I would have written (including more descriptive variable names, but that's just my preference).

I've changed:

  • the return values from adds() to better reflect the length and error conditions. Now it only returns -1 if it couldn't expand (and the original string is untouched) - any other return value is the new string length.
  • the return from myfree() if you want to really do want to set the string to NULL with something like "s = myfree (s)".
  • the checks in myfree() for NULL string since you can now never have an allocated string without an allocated string->strChars.

Here it is, use (or don't :-) as you see fit:

/*================================*/
/* Structure for storing strings. */

typedef struct _string {
    int  strLen;     /* Length of string */
    char *strChars;  /* Pointer to null-terminated chars */
} *string;

/*=========================================*/
/* Make a string, based on a char pointer. */

string make (char *srcChars) {
    /* Get the structure memory. */

    string newStr = malloc (sizeof (struct _string));
    if (newStr == NULL)
        return NULL;

    /* Get the character array memory based on length, free the
       structure if this cannot be done. */

    newStr->strLen = strlen (srcChars);
    newStr->strChars = malloc (newStr->strLen + 1);
    if(newStr->strChars == NULL) {     
        free(newStr);
        return NULL;
    }

    /* Copy in string and return the address. */

    strcpy (newStr->strChars, srcChars);
    return newStr;
}

/*======================================================*/
/* Add a char pointer to the end of an existing string. */

int adds (string curStr, char *addChars) {
    char *tmpChars;

    /* If adding nothing, leave it alone and return current length. */

    int addLen = strlen (addChars);
    if (addLen == 0)
        return curStr->strLen;

    /* Allocate space for new string, return error if cannot be done,
       but leave current string alone in that case. */

    tmpChars = malloc (curStr->strLen + addLen + 1);
    if (tmpChars == NULL)
        return -1;

    /* Copy in old string, append new string. */

    strcpy (tmpChars, curStr->strChars);
    strcat (tmpChars, addChars);

    /* Free old string, use new string, adjust length. */

    free (curStr->strChars);
    curStr->strLen = strlen (tmpChars);
    curStr->strChars = tmpChars;

    /* Return new length. */

    return curStr->strLen;
}

/*================*/
/* Free a string. */

string myfree (string curStr) {
    /* Don't mess up if string is already NULL. */

    if (curStr != NULL) {
        /* Free chars and the string structure. */

        free (curStr->strChars);
        free (curStr);
    }

    /* Return NULL so user can store that in string, such as
       <s = myfree (s);> */

    return NULL;
}

The only other possible improvement I could see would be to maintain a buffer of space and the end of the strChars to allow a level of expansion without calling malloc().

That would require both a buffer length and a string length and changing the code to only allocate more space if the combined string length and new chars length is greater than the buffer length.

This would all be encapsulated in the function so the API wouldn't change at all. And, if you ever get around to providing functions to reduce the size of a string, they wouldn't have to re-allocate memory either, they'd just reduce their usage of the buffer. You'd probably need a compress() function in that case to reduce strings that have a large buffer and small string.

paxdiablo
#4 was correct, but I chose this as correct for all the other points you made. Thank you. I do have a trunc() function to truncate a string, but it currently does a realloc() and then just sets the last character to '\0' - which works, but I like your suggestion of pre-malloc()ed space. Thank you!
Chris Lutz
+2  A: 
 tmp = realloc(s->_str, s->_len + l + 1);

realloc can return a new pointer to the requested block. You need to add the following line of code:

 s->_str = tmp;

The reason it doesn't crash in one case but does after adding one more character is probably just because of how memory is allocated. There's probably a minimum allocation delta (in this case of 16). So when you alloc the first 8 chars for the hello, it actually allocates 16. When you add the everyone it doesn't exceed 16 so you get the original block back. But for 17 chars, realloc returns a new memory buffer.

Try changing add as follows

 tmp = realloc(s->_str, s->_len + l + 1);
 if (!tmp) return 0;
 if (tmp != s->_str) {
     printf("Block moved!\n"); // for debugging
     s->_str = tmp;
 }
jmucchiello
+1  A: 

In function adds, you assume that realloc does not change the address of the memory block that needs to be reallocated:

tmp = realloc(s->_str, s->_len + l + 1);
if(!tmp) return 0;
memcpy(s->_str + s->_len, c, l);

While this may be true for small reallocations (because sizes of blocks of memory you get are usually rounded to optimize allocations), this is not true in general. When realloc returns you a new pointer, your program still uses the old one, causing the problem:

memcpy(s->_str + s->_len, c, l);
dmityugov