Posted Aug 14, 2005 2:26 UTC (Sun) by chad.netzer
(✭ supporter ✭
In reply to: calloc idiocy
Parent article: kzalloc()
Huh? What would be the semantics of a "hypothetical" calloc(4 * sizeof(t)), and how would they be different from the malloc(4 * sizeof(t))? Clearly bronson meant to compare malloc() to calloc()
bronson's point was that if using malloc() and calloc() are almost equivalent, differing by just an internal multiplication, there would seem to be little need for calloc(). It is an explicit indication that you are allocating an array (as you stated), and he argued that this was less readable for him.
malloc(n * sizeof(t))
are NOT equivalent. The calloc() is implemented as something like:
if (n && ((n*sizeof(t))/n == n))
p = malloc(n * sizeof(t));
p = NULL;
Assuming I haven't made an error, this protects against n being large enough to cause an overflow (or, for example, when n is an int, and it's a small negative number). The naive malloc() call could lead to serious complications, allocating a much smaller array than the caller expected.
Your macro example got garbled, so I won't comment on it.
to post comments)