CMD Simulator

malloc vs calloc - Dynamic Allocation

Learn the differences between malloc and calloc for dynamic memory allocation in C.

malloc vs calloc

Both allocate memory on the heap, but they differ in initialization.

malloc

  • malloc(size) allocates size bytes
  • Contents are uninitialized (garbage values)
  • Use when you will immediately overwrite the memory

calloc

  • calloc(count, size) allocates count * size bytes
  • Contents are zero-initialized
  • Use when you need a clean slate (e.g., arrays of zeros)