Is malloc() or calloc() "better"?
david wald
wald-david at CS.YALE.EDU
Wed Jan 4 07:34:42 AEST 1989
In article <9254 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>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?)
There is one possibility of a situation where calloc could be more
useful than malloc, however. In some virtual memory systems it is
possible to allocate memory filled with a zero bit pattern without
actually paging through the memory range. In theory, there are
applications where this would save time. I don't know how many
implementations actually do this, or how useful it is in those that do.
============================================================================
David Wald wald-david at yale.UUCP
waldave at yalevm.bitnet wald-david at cs.yale.edu
"A monk, a clone and a ferengi decide to go bowling together..."
============================================================================
More information about the Comp.lang.c
mailing list