I was wondering whether calloc()
is preferable to a malloc
followed by a memset
. The latter appears to be the most common way of allocating and initializing memory.
A github code search turns up many calloc
test and implementations but in the first number of pages no code actually using calloc
.
Does anyone who knows of any projects/organizations that use or recommend using calloc
and the circumstances where recommend it?
From the comments and answers below, here's some the thoughts that have emerges so far:
calloc(n, size)
can prevent overflow that is possible withmalloc(n * size)
combining malloc and memset gives calloc a chance to request a page that is known to already be zeroed.
a disadvantage to calloc that the combined steps may preclude other wrappers around malloc.