Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two arguments to calloc

Tags:

c

calloc

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?

like image 989
sanjoyd Avatar asked Nov 03 '10 03:11

sanjoyd


People also ask

How many arguments can calloc take?

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.

How many arguments are there in malloc?

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.

Why calloc () function is used in C programs?

“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'.

What is the argument to malloc?

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.


Video Answer


2 Answers

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.

like image 41
supercat Avatar answered Oct 07 '22 12:10

supercat


I heard two [mutually exclusive] explanations for why it has two arguments:

  1. 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).

  2. 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.

like image 162
AnT Avatar answered Oct 07 '22 12:10

AnT