C Programming

calloc() versus malloc()

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

   void * malloc( size_t size );

malloc() doesn’t initialize the allocated memory.

calloc() allocates the memory and also initializes the allocates memory to zero.

  void * calloc( size_t num, size_t size );

Unlike malloc(), calloc() takes two arguments: 1) number of blocks to be allocated 2) size of each block.

We can achieve same functionality as calloc() by using malloc() followed by memset(),

  ptr = malloc(size);
  memset(ptr, 0, size);

If we do not want to initialize memory then malloc() is the obvious choice.

Recent Comments


For any feedback,email us at feedback@geeksforgeeks.org

Report a Bug

For any report or bug,email us at support@geeksforgeeks.org