Implement calloc() on top of your allocator.
#include <stdint.h>
#include <string.h>
void *my_malloc(size_t n);
void *my_calloc(size_t count, size_t size)
{
if (size != 0 && count > SIZE_MAX / size) /* count * size would overflow */
return NULL;
size_t total = count * size;
void *p = my_malloc(total);
if (p) memset(p, 0, total);
return p;
}
Points: the multiplication overflow check is the whole reason calloc exists as a separate function; without it, calloc(0x10000, 0x10001) on 32-bit would allocate a tiny block and lead to a heap overflow. Real allocators skip the memset when the memory is known to be fresh zero pages from the OS.