Thursday 4 February 2016

Difference between calloc and malloc

calloc() versus malloc()

There are many explanations for the difference between calloc and malloc functions of C . Yet a lot of people fail to explain the major difference between the two functions. Well, here it is.

malloc() allocates memory block of given size(in bytes ) and returns a pointer to the beginning of a block


      void *malloc( size_t size );

calloc() pretty much does the same but takes two arguments, i) number of blocks to be allocated and ii) size of each block.
    void *calloc( size_t num, size_t size );
calloc() returns an array of object instead of single chunk of memory.

This difference is what is known to everybody. But the major difference between them is that malloc() does not initialize the allocated memory, where as calloc() initialize the allocated memory to zero.
malloc is used if we do not want to initialize memory. 

No comments:

Post a Comment