Is malloc() or calloc() "better"?

Doug Gwyn gwyn at smoke.BRL.MIL
Mon Jan 2 12:49:00 AEST 1989


In article <gables.416 at umigw.miami.edu> slores%gables.span at umigw.miami.edu (Stanislaw L. Olejniczak) writes:
>It seems to me that most programmers, in giving examples here, use malloc()
>instead of calloc().  It seems to me that, with the headache I always get
>thinking of C strings, calloc would be a more common procedure.  Would someone
>please enlighten me why is malloc so much more popular?

I don't understand the relevance to C strings..

Most often, once an object is allocated, it is filled with meaningful
contents.  Seldom is what calloc() would have provided the appropriate
contents for the object, so using calloc() would waste CPU time to no
advantage.  calloc() is only useful when you definitely want 0-valued
chars (NOT other data types) for the initial contents of a newly
allocated object.  (In non-portable applications, one can sometimes
use calloc() to obtain 0 values for other structure member types,
depending on the internal representation used by the architecture,
but why get in the habit of using non-portable kludges when the task
is easily enough done portably?)



More information about the Comp.lang.c mailing list