Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does calloc require two parameters and malloc just one?

Tags:

It's very bothersome for me to write calloc(1, sizeof(MyStruct)) all the time. I don't want to use an idea like wrapping this method and etc. I mean I want to know what two parameters gives me? If it gives something, why doesn't mallochave two parameters too?

By the way, I searched for an answer to this question but I didn't find a really good answer. Those answers was that calloc can allocate larger blocks than malloc can and etc.

I saw another answer that calloc allocates an array. With malloc I can multiply and I'll get an array and I can use it without 1, at the start.

like image 701
DividedByZero Avatar asked Sep 23 '12 20:09

DividedByZero


People also ask

Why does calloc have 2 arguments?

The calloc() function takes two arguments: the number of elements to allocate and the storage size of those elements. Typically, calloc() implementations multiply these arguments to determine how much memory to allocate.

How many parameters does a calloc function require?

The calloc() function does basically the same job as malloc(), except that it takes two parameters—the number of array elements and the size of each element—instead of a single parameter (which is the product of these two values). The allocated memory is also initialized to zeros.

What is the main difference between calloc () and malloc ()?

malloc() and calloc() functions are used for dynamic memory allocation in the C programming language. The main difference between the malloc() and calloc() is that calloc() always requires two arguments and malloc() requires only one.

Why do we use malloc instead of calloc?

Use malloc() if you are going to set everything that you use in the allocated space. Use calloc() if you're going to leave parts of the data uninitialized - and it would be beneficial to have the unset parts zeroed. 3.


2 Answers

Historical reasons.

At the time of when calloc was introduced, the malloc function didn't exist and the calloc function would provide the correct alignment for one element object.

When malloc was introduced afterwards, it was decided the memory returned would be properly aligned for any use (which costs more memory) and so only one parameter was necessary. The API for calloc was not changed but calloc now also returns memory properly aligned for any use.

EDIT:

See the discussion in the comments and the interesting input from @JimBalter.

My first statement regarding the introduction of malloc and calloc may be totally wrong.

Also the real reasons could also be well unrelated to alignment. C history has been changed a lot by compiler implementers. malloc and calloc could come from different groups / compilers implementers and this would explain the API difference. And I actually favor this explanation as the real reason.

like image 55
ouah Avatar answered Feb 08 '23 02:02

ouah


The only reason I could come up with is that

int *foo = calloc(42, sizeof *foo);

is one character shorter than

int *foo = malloc(42 * sizeof *foo);

The real reason is apparently lost to the millennia centuries decades of C history and needs a programming language archaeologist to unearth, but might be related to the following fact:

In contrast to malloc() - which needs to return a memory block aligned in accordance to the full block size - when using calloc() as intended, the memory block would only need to be aligned in accordance to the size passed as second argument. However, the C standard forbids this optimization in conforming implementations.

like image 30
Christoph Avatar answered Feb 08 '23 03:02

Christoph