malloc Vs calloc in c programming





In this tutorial we will discuss malloc Vs calloc. We already know that both malloc and calloc allocates memory at runtime.  The runtime allocation is also know as dynamic allocation. Apart from the memory allocation let us see the differences.

  1. The malloc library functions takes single argument while calloc function takes two arguments.
    void * malloc( size_t size ); 
    void * calloc( size_t num, size_t size );
  2. malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.
  3. memory allocated by calloc may/may not be contiguous, while malloc allocates memory in contiguous block if a contiguous block cannot be allocated then malloc would fail.
  4. malloc is faster than calloc and this is because calloc needs extra time to initialize memory.
  5. malloc allocates memory in contiguous block if a contiguous block cannot be allocated then malloc would fail while memory allocated by calloc may/may not be contiguous.

 

Learn C skills from our blogs:

http://wikistack.com/category/c-programming/

https://stackoverflow.com/questions/2214435/c-tutorial-question-relating-to-calloc-vs-malloc



Related Contents to follow



No Comments Yet

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.