Memory allocation / data access

Chris Torek torek at elf.ee.lbl.gov
Wed Mar 20 05:53:23 AEST 1991


In article <5654 at male.EBay.Sun.COM> harry.protoolis at uk.1.com writes:
>... why do you use malloc the first time and calloc the
>next ?, as they are both array allocations you should, strictly speaking, be
>using calloc for both.

Well, no: calloc(n,s) is *exactly* the same as

	malloc(n * s)

plus (if the malloc succeeds) a call to memset to set all n*s `char's
to '\0'.  If the objects you are allocating hold something other than
integral types, this `zeroing' is not useful since other types
(i.e., pointers and floating types) may not have all-bits-zero `0'
values.  For instance, some machines use `ring 7' for all nil pointers,
or a special bit for all floating point values meaning `do not trap
when this floating point value is examined'.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list