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

Doug Gwyn gwyn at smoke.BRL.MIL
Fri Jan 13 05:35:34 AEST 1989


In article <5003 at aldebaran.UUCP> jimp at cognos.UUCP (Jim Patterson) writes:
>I also agree that zero-filled memory isn't (maximally) portable, but I
>still don't know what machines it's not portable to.

Mark Brader has pointed out that the pANS imposes sufficient requirements
on integers of all kinds that calloc() will properly initialize them too.
Although ones-complement has -0, all-zero bits is +0, a valid zero value.
The same is true of IEEE floating-point; there is a -0.0 but all-zero bits
happens to be +0.0, a valid zero value.  I think I've seen floating-point
implementations where all-zero bits is not a valid representation of a
zero value, but I don't recall where.  (It is not required by the pANS.)

The main culprit is null pointers, since there have been implementations
where a null pointer was most conveniently represented by some pattern
other than all-zero bits.

None of this hides that fact that calloc() is a kludge.



More information about the Comp.lang.c mailing list