malloc 和 calloc 如何最终得到不同的签名?
有很多资源描述了 malloc
和 calloc
之间的功能差异,但我无法轻松找到描述其背后历史的资源。不同的功能签名:
void *calloc(size_t nmemb, size_t size);
void *malloc(size_t size);
当然,前面的size
是每个成员的大小。也许这个想法是可以通过操作系统延迟地完成多个页面大小的成员大小calloc
?
(我可以和下一个人一样编造理由——没有引用来源就没有可接受的答案。:-))
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 calloc
s 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. :-) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对 C 规范中使用的语言的解释有些开放。
这里的语言似乎是经过精心选择的 - malloc 在第 7.20.3.3 节中定义为:
早些时候,第 3.14 节中将对象定义为:
另一方面,
这应该很明显。 calloc 和 malloc 之间的区别在于,calloc 为 n 个概念对象的数组分配内存,尽管实际上这与调用 malloc 为 1 个大小为 (n * size) 的概念对象分配空间没有什么不同。
所以下一个问题是......有什么必要区分一组对象的空间和一个大对象的空间?
从程序员的角度来看,这两个函数调用只是要求不同的事情。一个请求分配一大块内存 - 我会处理它。另一个是说 - 我想要一个由 n 个大小相同的东西组成的数组,给我一些可以用于该数组的内存,然后将其归零 - 因为在大多数实现中,我可以对归零内存的含义做出很好的假设。
根据我对规范的阅读,您尝试将其用作 malloc + 归零这一事实是对该函数用途的误解。
他们最终得到了不同的签名,因为他们做了不同的事情。
一些可爱的想法...一个简单的 malloc 实现可以如下所示:
值得注意的是,如果我的 NULL 指针表示没有归零,那么 calloc 将不会返回 NULL 指针数组。更好的是,如果我设计一个整数表示,其中归零内存不是 ((int)0),calloc 不会返回 0 数组。
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:
Earlier an object was defined in section 3.14 as:
calloc on the other hand is defined in 7.20.3.1 as:
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:
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.