Is malloc() or calloc() "better"?
Dave Martindale
dave at onfcanim.UUCP
Thu Jan 5 09:49:14 AEST 1989
I haven't used calloc for years. I malloc a chunk of memory, then
explicitly initialize the things that need to be initialized.
This allows the reader to see exactly what the default values are,
and which fields need to be provided with values.
It also avoids a nasty portability bug: I've seen people who use
calloc to zero their allocated arrays or structures assume that
"all bits zero" is the proper value for floating-point zero -
definitely not true on all machines. An explicit initialization
to 0.0 is always correct.
More information about the Comp.lang.c
mailing list