PDA

View Full Version : Difference between malloc and calloc



anon10128
21-08-2013, 08:17 AM
Hi sir,
Is there any diffrence between malloc and calloc in terms of memory allocation?

The malloc() function allocates size bytes and returns a pointer to the allocated memory.
The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory.

This is from man page. I did not understand the difference between the two? Please help me.

Thank you.

anup
21-08-2013, 09:18 AM
Anon10128,

calloc stands for clear-and-allocate, think of it as malloc followed by zeroing out the entire memory region.

Thanks,
Anup