Possible Duplicate:
Does free(ptr) where ptr is NULL corrupt memory?
I'm writing a C function that frees a pointer if it was malloc()
ed. The pointer can either be NULL (in the case that an error occured and the code didn't get the chance to allocate anything) or allocated with malloc()
. Is it safe to use free(ptr);
instead of if (ptr != NULL) free(ptr);
?
gcc
doesn't complain at all, even with -Wall -Wextra -ansi -pedantic
, but is it good practice?