When this code executes the line
buffer = temporary;
two things happen. First, the memory that buffer
was holding is "leaked"; you no longer have that address to pass to free()
. Second, buffer
and temporary
now both point to a single piece of memory allocated by malloc()
or by realloc()
. Thus, when you call free()
on either buffer
or temporary
, you have freed that chunk of memory; to call free()
again would be a double free error.
I suggest something like this:
if (length >= buffer_size)
{
const size_t new_size = buffer_size + 1000;
char *temp = realloc(buffer, new_size);
if (!temp)
{
free(buffer);
/* handle the error in some way */
}
buffer = temp;
buffer_size = new_size;
}
Also, it is best practice to actually check for errors. malloc()
and realloc()
can fail.
Also, there isn't much point in allocating a single byte of memory for each buffer. You might as well start out by allocating 1000 bytes or whatever.
Also, the way you check to see if the buffer needs realloc()
seems needlessly tricky. I would suggest keeping a variable that tracks how long the buffer is, and call realloc()
when the buffer is not long enough, and then store into the buffer after the realloc()
.
EDIT: I think you got a bit confused about temporary
. Look at the code I put and you will see that I put the temp
variable inside the block of code of the if
statement. It's more obvious how long temp
will hold a useful value (not very long). Declaring temporary
at the outer scope, and also calling malloc()
to put a byte into it, is just confusing.