Path: utzoo!attcan!uunet!lll-winken!lll-lcc!ames!nrl-cmf!ukma!husc6!yale!wald-david From: wald-david@CS.YALE.EDU (david wald) Newsgroups: comp.lang.c Subject: Re: Is malloc() or calloc() "better"? Message-ID: <46857@yale-celray.yale.UUCP> Date: 3 Jan 89 21:34:42 GMT References: <9254@smoke.BRL.MIL> Sender: root@yale.UUCP Reply-To: wald-david@CS.YALE.EDU (david wald) Organization: Yale University Computer Science Dept, New Haven CT 06520-2158 Lines: 34 In article <9254@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) writes: >In article slores%gables.span@umigw.miami.edu (Stanislaw L. Olejniczak) writes: >>It seems to me that most programmers, in giving examples here, use malloc() >>instead of calloc(). It seems to me that, with the headache I always get >>thinking of C strings, calloc would be a more common procedure. Would someone >>please enlighten me why is malloc so much more popular? > >I don't understand the relevance to C strings.. > >Most often, once an object is allocated, it is filled with meaningful >contents. Seldom is what calloc() would have provided the appropriate >contents for the object, so using calloc() would waste CPU time to no >advantage. calloc() is only useful when you definitely want 0-valued >chars (NOT other data types) for the initial contents of a newly >allocated object. (In non-portable applications, one can sometimes >use calloc() to obtain 0 values for other structure member types, >depending on the internal representation used by the architecture, >but why get in the habit of using non-portable kludges when the task >is easily enough done portably?) 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 theory, there are applications where this would save time. I don't know how many implementations actually do this, or how useful it is in those that do. ============================================================================ David Wald wald-david@yale.UUCP waldave@yalevm.bitnet wald-david@cs.yale.edu "A monk, a clone and a ferengi decide to go bowling together..." ============================================================================