Calloc v.s. Malloc

19 Jul 2015

It’s been a while since my last post. Let’s restart with something light.

To know the answer, you can read the manual: man malloc.

But basically malloc is NOT backed by real memory allocation in OSes (like Linux) with optimistic memory allocation strategy until the program actually touches that part of memory. That also means the memory chunk ‘allocated’ is not initialized.

calloc on the other hand touches the memory by writing zeros (initializing) to it and therefore is backed by real memory on RAM.

Since calloc does more real work than malloc, including actually finding an available memory block and initializing it, calloc is also slower.