This is in part historical, and in part as a useful feature
We had heap with uninitialised memory (which was not zero), so more quick to get memory. Then this was discovered that this is a security problem, so much later, you get memory cleared to zero (and never memory set by other processes). Do not assume all systems will do it (especially on small embedded CPU, where CPU and bus access are expensive (time and power).
calloc
is very handy, when you allocate arrays (as you see, the signature is done for arrays). Often on arrays, you want to initialize values to zero. A loop is very slow, and static
had already the initialization to zero. We have two possibilities: initialized memory with calloc
or uninitialised memory with malloc
.
Note: malloc
doesn't guarantee to give you all zeros: it could give you already allocated (from your process) and freed memory. Just now new memory given by kernel is zeroed (e.g. with brk
/sbrk
, which is sometime called by malloc
/calloc
, in case of lack of free memory in existing heap memory). These are two different allocation of memory.