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

Steve Summit scs at adam.pika.mit.edu
Fri Jan 6 14:56:29 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().  Would someone
>please enlighten me why is malloc so much more popular?

calloc is essentially worthless.  The zero-fill operation it
provides is rarely useful in portable programs, for reasons which
have already correctly been mentioned.  I advocate replacing any
contemplated call to

	calloc(n, s)
with
	malloc(n * s)

followed by explicit initialization and/or zero fill, if
required.

I wish calloc had never been standardized; its incremental
utility is quite low.  (ANSI probably had no choice, though,
since existing code must be protected.)  There are claims that
calloc can be "more efficient" (there's that word again) under
contorted circumstances on virtual memory machines with demand-
zero pages, but we went through all that on comp.std.c a month or
two ago, and it doesn't bear repeating here.

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.lang.c mailing list