malloc 和 calloc 如何最终得到不同的签名?

发布于 2024-12-06 22:34:31 字数 549 浏览 2 评论 0原文

可能的重复:
为什么 calloc 采用两个参数,而 malloc 仅采用一个参数?

很多资源描述了 malloccalloc 之间的功能差异,但我无法轻松找到描述其背后历史的资源。不同的功能签名:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2024-12-13 22:34:31

这对 C 规范中使用的语言的解释有些开放。

这里的语言似乎是经过精心选择的 - malloc 在第 7.20.3.3 节中定义为:

malloc函数为大小为的对象分配空间
由大小指定,其值是不确定的。

早些时候,第 3.14 节中将对象定义为:

执行环境中的数据存储区域,
其内容可以代表值

另一方面,

calloc 在 7.20.3.1 中定义为:

calloc 函数为 nmemb 对象数组分配空间,
每个尺寸都是尺寸。空间被初始化为所有位
零。

这应该很明显。 calloc 和 malloc 之间的区别在于,calloc 为 n 个概念对象的数组分配内存,尽管实际上这与调用 malloc 为 1 个大小为 (n * size) 的概念对象分配空间没有什么不同。

所以下一个问题是......有什么必要区分一组对象的空间和一个大对象的空间?

从程序员的角度来看,这两个函数调用只是要求不同的事情。一个请求分配一大块内存 - 我会处理它。另一个是说 - 我想要一个由 n 个大小相同的东西组成的数组,给我一些可以用于该数组的内存,然后将其归零 - 因为在大多数实现中,我可以对归零内存的含义做出很好的假设。

根据我对规范的阅读,您尝试将其用作 malloc + 归零这一事实是对该函数用途的误解。

他们最终得到了不同的签名,因为他们做了不同的事情。


一些可爱的想法...一个简单的 malloc 实现可以如下所示:

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

值得注意的是,如果我的 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文