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

david wald wald-david at CS.YALE.EDU
Fri Jan 6 14:15:22 AEST 1989


In article <9339 at ihlpb.ATT.COM> nevin1 at ihlpb.UUCP (55528-Liber,N.J.) writes:
>In article <46857 at yale-celray.yale.UUCP> wald-david at CS.YALE.EDU (david wald) writes:
>|
>|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 this case, wouldn't malloc() just call the same routine as calloc()?
>Since calloc() always has to do more work than malloc(), malloc() should
>always get the best performance.

It might be that malloc() would skip whatever step performed the "fill",
where the fill operation actually consists of setting some tag
indicating the default value for all bytes in the page.  calloc() would
still be slightly less efficient than malloc(), but much, much more
efficient than zeroing the memory "by hand."

Besides, if portability is an issue, you can't depend on malloc() doing
that zeroing operation, even if it's the same as calloc() in whatever
implementation you're using.

As I said in my original posting, I don't know of any actual
implementations that do this, although I'm told there are some where
it's possible.  Does anyone have any hard data on this?


============================================================================
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