Is calloc just a wrapper around malloc?

05 Jan 2022 - John Z. Li

There are two functions in C to allocate memory on the heap. One is maloc, which has the following signature:

void* malloc (size_t size);

It takes the size of the memory block that the user wants to allocate as a parameter, and return a voud pointer to the allocated memory block on success, otherwise a null pointer will be returned. The other heap allocation function in C is calloc, which has the following signature:

void* calloc (size_t num, size_t size);

calloc receives two parameters. On success, it will return a void * pointer to a memory block which has the size of num*size. Furthermore, each bits of the memory block is set to zero (each byte is set to \0).

It is tempted to think that calloc is just a convenient wrapper of malloc by calling malloc with parameter num*size followed a call of memset setting each byte to \0. In other words, it might be assumed that calloc is implemented as below:

void* calloc (size_t num, size_t size){
	size_t n = num*size;
	void* p = malloc(n);
	if(p)
	memset(p, '\0', n);
	return p;
}

Actually, many online tutorials teaches that this is how calloc works.

However, the answer to the above question is No. There are two reasons: