It is good practice to not bother checking for NULL before calling free. Checking just adds unnecessary clutter to your code, and free(NULL) is guaranteed to be safe. From section 7.20.3.2/2 of the C99 standard:
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs.
As noted in the comments, some people sometimes wonder if checking for NULL is more efficient than making a possibly unnecessary function call. However, this:
- Is a premature micro-optimization.
- Shouldn't matter. Checking for
NULL first might be a pessimization. For example, if 99% of the time your pointers aren't NULL, then there would be an extra NULL check 99% of the time to avoid an extra function call 1% of the time.