Why does calloc
take two arguments instead of one like malloc
?
Specifically, since there is no difference between (or is there?) between the following expressions:
calloc (a, b); calloc (b, a); calloc (a * b, 1); calloc (1, a * b);
why not just accept the total number of bytes to allocate? What is the rationale behind this interface? And why does this not apply to malloc?
malloc() takes a single argument (the amount of memory to allocate in bytes), while calloc() takes two arguments — the number of elements and the size of each element.
malloc() takes a single argument, which is the number of bytes to allocate. Unlike malloc(), calloc() takes two arguments: 1) Number of blocks to be allocated. 2) Size of each block in bytes.
“calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. it is very much similar to malloc() but has two different points and these are: It initializes each block with a default value '0'.
The argument to malloc is the number of bytes to be allocated. If you need space for an array of n elements of type T , call malloc(n * sizeof(T)) . malloc does not know about types, it only cares about bytes.
I believe that malloc is guaranteed to return an area of memory which is aligned according to the coarsest requirement that would be compatible with the size indicated by the second argument. For example, if the system requires alignment of 2 and 4-byte integers, and the second argument is 10, the returned pointer must be aligned on a two-byte boundary; if the second argument were 12, the pointer would be aligned on a four-byte boundary. I suspect that in practice many systems will align all returned pointers to the largest possibly-required boundary, regardless of size, but I don't think it's required except for calloc.
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 overflows size_t
), calloc
returns null pointer to indicate failure. With malloc
you have to watch for overflow yourself, which many people simply forget to do. (Although the history of standard library knows examples of calloc
implementations that ignored overflow, and thus worked incorrectly).
calloc
actually allows one to allocate bigger blocks of memory than the range of type size_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, since calloc
uses two arguments of type size_t
, it can allocate bigger blocks than malloc
will ever be able to (since malloc
takes only one argument of type size_t
).
I always believed that the first explanation is the right one. However, after reading some posts here on SO I have my doubts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With