Thread: Why use calloc()?

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057

    Why use calloc()?

    I was wondering what calloc() is good for (besides setting everything to zero). As far as I can see,
    Code:
    calloc(x, y);
    is the same as
    Code:
    malloc(x*y);
    (Except for calloc setting the allocated memory to zero.)

    So what is calloc() good for?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    So what is calloc() good for?
    Like you said, for initializing the allocated memory to 0...

    If you wanted to you could live your entire life without using calloc() by just doing:
    Code:
    void *ptr = malloc(SIZE);
    memset(ptr, 0, SIZE);
    ...instead.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's what I thought . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    On some systems the calloc call could be optimized better for getting arrays of zeroed bytes. The system can keep a list of zeroed out memory and non-zeroed memory so calloc can look up memory in the zero pool while malloc returns pointers to the other pool and the memset is extra overhead that was already done on an idle moment for the zero pool.

    Windows does that for instance. When the system is idle it starts the zero page thread. I don't know about calloc implementations that make use of it thuogh.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Last edited by Salem; 07-20-2005 at 05:28 AM. Reason: typo
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    So it initialises to zero not NULL?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > So it initialises to zero not NULL?
    Yes, calloc is nothing more than malloc + memset - the result is a block of memory with all bits set to 0.

    But nothing in C insists that NULL (as a pointer) has to be represented by all-bits-zero
    http://www.eskimo.com/~scs/C-faq/s5.html

    void *p = NULL;
    if ( p == NULL )
    if ( p == 0 )
    These will always be true, whether your machine uses all-bits-zero for a NULL pointer or not.

    void *p; memset( &p, 0, sizeof(void*) ); // just what calloc would do
    if ( p == NULL )
    if ( p == 0 )
    These will work on some machines, and not on others.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Salem
    void *p; memset( &p, 0, sizeof(void*) ); // just what calloc would do
    if ( p == NULL )
    if ( p == 0 )
    These will work on some machines, and not on others.
    Do you know of any system where this is true btw? I'm not questioning the portability issue, just curious.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  2. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM
  5. calloc vs malloc
    By Jubba in forum C Programming
    Replies: 2
    Last Post: 02-21-2002, 04:54 PM