Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How did malloc and calloc end up with different signatures? [duplicate]

Tags:

Possible Duplicate:
Why calloc takes two arguments while malloc only one?

There are lots of resources describing the difference in functionality between malloc and calloc, but I can't easily find one that describes the history behind the differing function signatures:

   void *calloc(size_t nmemb, size_t size);    void *malloc(size_t size); 

Of course, the size in the former is the size for each member. Maybe the idea was that multiple-of-the-page-size member size callocs could be done lazily via the operating system?

(I can make up reasons as well as the next guy -- no accepted answers without cited sources. :-) )

like image 843
cdleary Avatar asked Sep 28 '11 09:09

cdleary


People also ask

Why does calloc have two 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.

Why is calloc more secure than malloc?

Calloc is also better and faster than malloc and manual zero the memory for very large memory. @jclin calloc() vs. malloc() & memset() performance can be deceiving as calloc() can simple defer the real zeroing until later.

How many Paramters malloc () Functionrequires?

In C, dynamic memory is allocated from the heap using some standard library functions. The two key dynamic memory functions are malloc() and free(). The malloc() function takes a single parameter, which is the size of the requested memory area in bytes.

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.


1 Answers

This is somewhat open to interpretation of the language used in the C specification.

The language here seems very carefully chosen - malloc is defined in section 7.20.3.3 as:

The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

Earlier an object was defined in section 3.14 as:

region of data storage in the execution environment, the contents of which can represent values

calloc on the other hand is defined in 7.20.3.1 as:

The calloc function allocates space for an array of nmemb objects, each of whose size is size. The space is initialized to all bits zero.

This should make it obvious. The difference between calloc and malloc is that calloc allocates memory for an array of n conceptual objects, although in practice this is no different from a call to malloc which allocates space for 1 conceptual object of size (n * size).

So the next question is... what is the need for the distinction between space for an array of objects and space for one large object?

From a programmers perspective the two function calls are just asking for different things. One is asking for a big hunk of memory to be assigned - and I'll deal with it. The other is saying - I want an array of n things which are of this size, give me some memory which I could use for this array, and zero it - because in most implementations I can make good assumptions about what zeroed memory means.

The fact that you are trying to use it as malloc + zeroing is, by my reading of the specification, a misunderstanding about the purpose of the function.

They ended up with different signatures because they do different things.


Some cute side thoughts... A trivial malloc implementation can look like:

#define malloc(x) (calloc(1, x)) 

It is also fun to note that if my NULL pointer representation is not zeroed, then calloc won't return me an array of NULL pointers. Better yet, if I design an integer representation in which zeroed memory is not ((int)0), calloc won't return me an array of 0s.

like image 141
James Greenhalgh Avatar answered Jan 03 '23 01:01

James Greenhalgh