/* If you are going to use realloc, don't make this common mistake! */
#include <stdlib.h>
int main (void) {
size_t size = 16;
char * ptr = malloc(size);
size = 32;
/* This is wrong!
ptr = realloc(ptr, size);
ptr is allocated. If this call fails ptr will be set to NULL, and
the allocated memory will not be freed.
*/
char * tmp_ptr = realloc(ptr, size);
if (NULL != tmp_ptr) {
ptr = tmp_ptr;
} else {
/* This is the error case */
free(ptr);
ptr = NULL;
}
free(ptr);
}