Thread: Malloc And Calloc

  1. #1
    Registered User
    Join Date
    Jul 2007

    Malloc And Calloc

    Can anyone plz tell what is the difference between malloc and calloc?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Google, use the man pages, check wikipedia..... Summed up:


    • Allocate memory dynamically.
    • Return NULL if contiguous block of memory matching the size requested could not be allocated.


    • malloc() just takes the size in bytes as a size_t argument, while calloc() wants two arguments, one being the size of the element this memory is being requested for, and the other being the amount of elements that should be able to fit in this block of memory (ie. an element could be an int, and you want to fit 4 ints into this block of memory).
    • If calloc() succeeds, it sets the value for each byte of allocated memory to 0. malloc() is not guarenteed to do such, but some compilers might do this, notably I think Visual C++ in debug mode does this. Never assume such for malloc().

    This was asked already, so you might want to search the last page or two of this section of the forums.

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. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:57 AM
  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