I heard two [mutually exclusive] explanations for why it has two arguments:
-
calloc
takes the responsibility for checking for overflow on multiplication. If the total size of the requested block is too large (like overflowssize_t
),calloc
returns null pointer to indicate failure. Withmalloc
you have to watch for overflow yourself, which many people simply forget to do. (Although the history of standard library knows examples ofcalloc
implementations that ignored overflow, and thus worked incorrectly). -
calloc
actually allows one to allocate bigger blocks of memory than the range of typesize_t
, i.e.calloc
might be capable of performing the proper non-overflowing large multiplication of its arguments and allocate the block of the resultant size. For this reason, sincecalloc
uses two arguments of typesize_t
, it can allocate bigger blocks thanmalloc
will ever be able to (sincemalloc
takes only one argument of typesize_t
).
I always believed that the first explanation is the right one. However, after reading some posts here on SO I have my doubts.