"calloc"中第一个"c"代表什么?

What does the first "c" stand for in "calloc"?

有同学问的,我也不是很清楚。

猜测包括:"counted"、"clearing"、"chunked"、"complete"、...

标准库文档没有说明它代表什么,也没有类似命名的函数来指示模式。有谁知道实际的词源并且可能有权威的参考来支持它?

根据 Linux System Programming 一书(作者 Robert Love)的摘录,calloc.

的词源不存在任何官方来源

一些似是而非的候选人似乎是:

  1. Countcounted,因为 calloc 采用单独的计数参数。
  2. Clear,因为它确保返回的内存块已经被清除。

    • 据报道,Brian Kernighan 相信 "c" 代表 clear(尽管他承认他不确定)。
    • (见评论。) 早期的 calloc.c seems to contain an explicit reference to the word clear in a source code comment (but no reference to the word count or to any other candidate). In another source code comment in the file malloc.c,单词 clear 再次出现,参考单词 calloc
  3. C,如C语言中

    • (参见 和评论。) 可能是大约同时引入的一组函数的命名约定。

calloc = 连续内存分配。

这意味着根据 calloc() 的语法,即

void *calloc (size_t number_of_blocks, size_t size_of_each_block_in_bytes);   

它接收两个参数:否。块的数量和一个块的大小,因此它为编号分配了一个内存数组。您将提供的块数。

我想没有人知道。但是用必须清除内存的语义来描述 calloc() 调用,而不是 malloc(内存分配)returns 之前的 free() 操作遗留下来的任何随机垃圾,是有用的 modus operandi 对于学生来说,这很有用,因为它提醒用户 malloc() returns 是一个不安全的值。

我做了一些研究,在 "UNIX@ TIME-SHARING SYSTEM 中发现了以下内容: UNIX 程序员手册。第七版,第 2 卷”,第 "PROGRAMMING" 章(我用斜体字):

char *malloc(num);

allocates num bytes. The pointer returned is sufficiently well aligned to be usable for any purpose. NULL is returned if no space is available.

char *calloc(num, size);

allocates space for num items each of size size. The space is guaranteed to be set to 0 and the pointer is sufficiently well aligned to be usable for any purpose. NULL is returned if no space is available.

 cfree(ptr) char *ptr;

Space is returned to the pool used by calloc. Disorder can be expected if the pointer was not obtained from calloc.

  • 最后一句话清楚地表明 calloc() 肯定(本来是?)malloc() 有更多不同 然后只需清除内存即可。

    有趣的是,在那几百页中没有提到 free() ... :-)

  • 而且UNIX V6已经有了calloc() which calls alloc()。 (链接的)源代码没有显示任何将任何内存归零的方法。

从以上两个事实得出结论,我强烈反对calloc()中领先的"c"代表"clear"的理论。

正如 Anant 的回答所说,它代表 Contiguous Allocation。每

Rather than allocating from a compiled-in fixed-sized array, malloe will request space from the operating system as needed. Since other activities in the program may also request space without calling this allocator, the space that malloe manages may not be contiguous. Thus its free storage is kept as a list of free blocks. Each block contains a size, a pointer to the next block, and the space itself. The blocks are kept in order of increasing storage address, and the last block (highest address) points to the first.

C 编程语言第 8.7 节,K&R,第 2 版