开发者

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

开发者 https://www.devze.com 2023-04-08 18:44 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Why calloc takes two arguments while malloc only one?
This question already has answers here: Closed 11 years ago.

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, si开发者_运维问答ze_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. :-) )


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号