Difference Between calloc, malloc, and realloc

Question: What is the difference between malloc(), calloc(), and realloc()

malloc, calloc, and realloc are functions used for memory allocation in C/C++ languages. There are some fundamental differences on how the above functions work.
realloc()
First of all realloc() is actually a reallocation function. It is used to resize a previously allocated (using malloc(), calloc(), or realloc()) block of memory to the desired size. Depending on whether the new size if less or more than the original size the block may be moved to new location.

Usage and example of realloc()

Function Prototype for realloc():
void *realloc(void *pointer, size_t size);
  

malloc() vs calloc()


There two basic difference between malloc() and calloc() functions:
1. malloc() allocates memory in bytes. So the programmer specifies how many bytes of memory malloc should allocate and malloc will allocate that many bytes (if possible) and return the address of the newly allocated chunk of memory.
 
Function prototype for malloc():
void *malloc(size_t size); //size is the number of bytes to allocate
 
On the other hand calloc() allocates a chunk of memory specified by a block/element size and the number of blocks/elements. 
Function prototype for calloc():

void *calloc(size_t nelements, size_t elementSize);

2. malloc() does not initialize memory after it allocates it. It just returns the pointer back to the calling code and the calling code is responsible for initialization or resetting of the memory, most probably by using the memset() function. On the other hand calloc() initializes the allocated memory to 0. calloc() is obviously slower than malloc() since it has the overhead of initialization, so it may not be the best way to allocate memory if you don't care about initializing the allocated memory to 0.

Side note: Don't forget to free your allocated memory when you are done with it using the free() function.

Must Read Books for Technical/Programming Interview Preparation

Coding, Program Design and Interview Questions:
  • Programming Interviews Exposed: Secrets to Landing Your Next Job ~ John Mongan, Noah Suojanen, and Eric Giguère
  • Programming Pearls (2nd Edition) ~ Jon Bentley
  • Expert C Programming: Deep C Secrets ~ Peter van der Linden
  • Puzzles for Programmers and Pros ~ Dennis Shasha
  • More Programming Pearls: Confessions of a Coder ~ Jon Bentley
  • Programming Challenges ~ Steven S. Skiena, Miquek Revilla

Data-Structures and Algorithms:
  • Algorithms in C, Parts 1-5 (Bundle): Fundamentals, Data Structures, Sorting, Searching, and Graph Algorithms (3rd Edition) ~ Robert Sedgewick
  • Head First Object-Oriented Analysis and Design ~ Brett D. McLaughlin, Gary Pollice, and Dave West

Design:
  • Head First Design Patterns ~ Elisabeth Freeman, Eric Freeman, Bert Bates, and Kathy Sierra
  • Head First Object-Oriented Analysis and Design ~ Brett D. McLaughlin, Gary Pollice, and Dave West

Software:
  • Writing Secure Code, Second Edition ~ Michael Howard and David LeBlanc
  • Code Complete: A Practical Handbook of Software Construction ~ Steve McConnel

Puzzles:
  • How would you move mount fuji? ~ William Poundstone
  • Aha! Insight ~ Martin Gardner