Re: Difference?

From: Mark McIntyre (markmcintyre_at_spamcop.net)
Date: 02/26/04


Date: Wed, 25 Feb 2004 23:11:08 +0000

On 24 Feb 2004 21:19:07 -0800, in comp.lang.c , mohanasundaram@msn.com
(Mohanasundaram) wrote:

>Hi All,
>
> After reading all the postings in this thread I have a query in
>my mind. If there is no big difference between malloc and calloc then
>why there exists two functions for memory allocation which can be
>achieved by one sigle function

Since zeroing out memory is often a Good Thing:
malloc might be considered fast-but-dirty.
calloc might be considered slow-but-safe.

This is a simplification, and of course you can achieve calloc with a
malloc/memset pair. but we assume that the compiler writer has access to
some optimised way of doing calloc which is more efficient than
malloc/memset, and since memory operations are often heavily used in C,
optimised ways to do them are a Good Thing too.

By the way, you could use the same logic for putc & puts, scanf & sscanf,
and probably quite a few other C features - including integer
multiplication....

-- 
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


Relevant Pages

  • Re: malloc vs calloc
    ... malloc and calloc differs in the way they ... allocate memory(malloc give a contigous block, ...
    (comp.lang.c)
  • Re: Difference?
    ... >> What is the difference between malloc and calloc other than the ... >> come across tells that calloc initialize the memory to all zeros. ... if the allocation succeeds is suitably aligned so that it may be assigned ...
    (comp.lang.c)
  • Re: far pointers
    ... > a hammer or a wrench? ... Right, if you are going to malloc() some space then set it all to zero, you ... might as well just use calloc(). ... If you don't think you can handle using your memory properly, ...
    (comp.lang.c)
  • Re: malloc vs calloc
    ... malloc and calloc differs in the way they ... allocate memory(malloc give a contigous block, ...
    (comp.lang.c)
  • Re: reformatting strings and memory issue
    ... calloc is what is not succeeding. ... > memory that doesn't belong to you can't count on anything anymore. ... > in malloc(), freeetc, since these functions are often implemented in ... does memory stuff it is system specific (to POSIX/Unix systems, ...
    (comp.lang.c)