malloc和释放calloc怎么会用不同的签名? [英] How did malloc and calloc end up with different signatures?

查看:138
本文介绍了malloc和释放calloc怎么会用不同的签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/7536413/why-calloc-takes-two-arguments-while-malloc-only-one\">Why释放calloc需要两个参数,而malloc的只有一个?

地段的说明的malloc 释放calloc 之间的功能差异的资源,但我不能很容易地找到一个描述了不同的函数签名背后的历史:

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);

当然,尺寸前者是每个成员的大小。也许这个想法是多重的最页大小尺寸会员可以callocs懒洋洋地通过操作系统来实现?

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 OS?

(我可以弥补的原因,以及未来的家伙 - 没有公认的答案,而不引用来源: - )

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

推荐答案

这是在C规范使用的语言间pretation有所开放。

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

这里的语言似乎很认真选择 - malloc的是在第7.20.3.3的定义是:

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

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

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

此前的对象是在3.14节定义为:

Earlier an object was defined in section 3.14 as:

在执行环境中的数据存储的区域,该
  其中的内容可以重新present值

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

另一方面释放calloc在7.20.3.1所定义:

calloc on the other hand is defined in 7.20.3.1 as:

在释放calloc功能nmemb个对象的数组分配空间,
  每个其尺寸是尺寸。该空间被初始化为所有位
  零。

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.

这应该很明显。释放calloc和malloc的之间的不同之处在于释放calloc对于n概念对象数组分配内存,尽管在实践中,这是从对malloc呼叫这对于尺寸为1概念性对象分配空间没有不同(正*大小)。

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?

从程序员的角度来看这两个函数调用只是要求不同的事情。一个是要求大内存大块被分配 - 我会处理的。另一种是说 - 我想要的n个事物的数组这是这种规模的,给我一些记忆,我可以使用这个数组和零它 - 因为在大多数实现我可以做什么清零的内存意味着良好的假设

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.

这是您要使用它的malloc +调零,通过我的阅读说明书,关于该功能的目的误解的事实。

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.

有些可爱的一面的想法...一个平凡的malloc实现可以如下:

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

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

这也是有趣的注意的是,如果我的NULL指针重新presentation不归零,然后释放calloc将不会返回NULL我一个指针数组。更妙的是,如果我重新设计在清零的内存presentation整数不是((INT)0),释放calloc将不会返回我0的数组。

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.

这篇关于malloc和释放calloc怎么会用不同的签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆