Lately I've adopted a style where free() and destructor functions (i.e. foo_
destroy(foo_t *foo)) are expected to be no-op for NULL pointer. It's quite 
useful, you just need to get used to it so that the behavior does not 
surprise you. So I would agree that if (p) free(p) is not needed. If found 
in HelenOS code, that's for historical reasons because this feature of free 
is a somewhat recent addition to HelenOS.

Also note I prefer to check for non-null pointers by if (p != NULL) rather 
than if (p) (basically by pretending that if only accepts a boolean argument
and that boolean is a strict type). However I do know that there are many C 
programmers (especially those familiar with C++) that feel exactly opposite.

Cheers,
Jiri

Jakub wrote:
"On 03/30/2016 03:54 PM, Jan Vesely wrote:
> On Wed, 2016-03-30 at 13:21 +0200, Manuele Conti wrote:
>> Hi All,
>> I found a memory leak in a mkmfs util and I try to fix it.
> 
> free() is NULL safe (C std 7.20.3.2, and also in HelenOS
> uspace/lib/c/generic/malloc.c:972). doing:
> 
> if (p)
> free(p);
> 
> is useless.

But may result in faster code :-) Anyway, a little bit of explicitness
here and there doesn't hurt.

Jakub

_______________________________________________
HelenOS-devel mailing list
[email protected]
http://lists.modry.cz/listinfo/helenos-devel";
_______________________________________________
HelenOS-devel mailing list
[email protected]
http://lists.modry.cz/listinfo/helenos-devel

Reply via email to