On 04/01/2014 09:28 AM, Ondrej Oprala wrote:
Hi,
Could someone please explain to me, why are xrealloc and xfree (or more
specifically - their parts) from lib/malloc/xmalloc.c necessary?
e.g.
if (string)
free(string);
Why is this needed? AFAIK the C standard specifies free(NULL) is
completely legal.
Bruno Haible argued that the cost of a redundant if conditional is
cheaper than the cost of a function call in profiling runs, at least in
the portions of gnulib where he used that idiom. Personally, I like
getting rid of the redundant if (I think the extra CPU cycles spent are
in the noise, until given a profile dump that proves otherwise).
Also:
temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
Again, the C standard says that realloc(0, bytes) is equivalent to
malloc(bytes).
There's no good reason for this one; probably just extremely old legacy
code back in the day when various libc were not compatible to the
restrictions added in later versions of the C standard.