You shouldn’t assign realloc() call result to the same variable that pointed to original memory block.

In case of failure the original pointer will not be released so you have to do it manually!
/* BAD! */
p = (char*) realloc(p, new_size);
if(!p) {
    return;
}
/* Good */
old_p = p;
p = (char*) realloc(old_p, new_size);
if(!p) {
    free(old_p);
    return;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s