Hi Jim, > > Nowadays little code remains that is written for K&R C, and few minds > > program > > for K&R C. Therefore the "if before free" cases that are remaining are > > more likely to be intentional than before. > > If only that were true. I've just removed several hundreds of > such uses from glusterfs. From what I've seen, the majority of > C/C++ developers attempt to avoid free(p) when p is NULL.
For C hackers, the p != NULL test may be intentional: they may want to save a function call. Whereas C++ programmers often add this test for a different reason: C++ APIs are often not well documented (because C++ tends to make you create huge, thoughtless APIs, not small and intelligent APIs), and so the caller of a function never really knows whether NULL is a possible return value. When you have gotten a core dump because your invocation of a small function returned NULL instead of an empty list, 5 or 10 times in the same week, you learn to add if (p != NULL) protections everywhere, even where it's not needed. Bruno
