I had forgotten about calloc for the longest time, but I was recently reminded of it. Now i'm curious, what would be the difference between malloc(numElems * elemSize); and calloc(numElems, elemSize); ? If my knowledge of arrays is correct, there shouldn't be any difference in the memory allocated.

Recommended Answers

All 3 Replies

calloc initializes the memory with zero. calloc = clear + alloc

Conceptually, calloc works like this:

void *calloc ( size_t n, size_t size )
{
  void *mem = malloc ( n * size );

  memset ( mem, 0, n * size );

  return mem;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.