ProAnswers.org

What is the difference between malloc and calloc?

Malloc -

  1. Used to allocate the contiguous memory space for a chunk of bytes.
  2. Contains the garbage value by default.
  3. Returns NULL pointer if failed to allocate the memory else return the Pointer pointing to the memory block.
    4.take one argument i.e(malloc(sizeof(int)*10) and allocate bytes of memory.

Calloc -

  1. Used to allocate the contiguous collective memory space.
  2. Contains 0 value by default.
  3. Returns NULL pointer if failed to allocate the memory else return the Pointer pointing to the memory block.
    4.take two argument i.e(calloc(no.of.var size of each var) and allocate block of memory.

The memory allocated in both the cases will be contiguous only else both fail if chunk not available.

Calloc is equivalent to :
malloc()+ memset(to zero)

Malloc and Calloc are functions provided to allocate memory at run time.Both of these functions return pointer to first block of allocated memory in success and return null in case of failure.

Found good article that explores the difference between malloc and calloc [Here](http://swsharinginfo.blogspot.com/2011/11/difference-between-malloc-and-calloc-in.html)