59

A student asked the question and I didn't know for sure.

Guesses include: "counted", "clearing", "chunked", "complete", ...

The standard library documentation doesn't say what it stands for and there aren't similarly named functions that would indicate a pattern. Does anyone know the actual etymology and perhaps have an authoritative reference to back it up?

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • Taken from `man 3 calloc`: `void *calloc(size_t nmemb, size_t size);` - The `calloc()` function allocates memory for an array of `nmemb` elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. If `nmemb` or size is 0, then `calloc()` returns either NULL, or a unique pointer value that can later be successfully passed to `free()`. – gengisdave Aug 08 '15 at 09:35

5 Answers5

61

According to an excerpt from the book Linux System Programming (by Robert Love), no official sources exist on the etymology of calloc.


Some plausible candidates seem to be:

  1. Count or counted, because calloc takes a separate count argument.
  2. Clear, because it ensures that the returned memory chunk has been cleared.

    • Brian Kernighan is reported to believe that the "c" stands for clear (although he has admitted he's not sure).
    • (See comments.) An early 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, the word clear appears again, in reference to the word calloc.
  3. C, as in the C language.

    • (See alk's answer and comments.) Possibly a naming convention for a set of functions that were introduced at about the same time.
Community
  • 1
  • 1
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
  • 21
    The V7 (the version that added `malloc()` and `calloc()` to Unix) source code [uses clear in a comment](https://github.com/dspinellis/unix-history-repo/blob/Research-V7/usr/src/libc/gen/calloc.c) and actually clears the allocated memory block. – cremno Aug 08 '15 at 00:43
  • 2
    @cremno Wow, that's an extremely useful reference repository. And it probably gets us as close to an answer as we can get. I've edited the answer to include your link. – Theodoros Chatzigiannakis Aug 08 '15 at 00:55
  • 5
    And for what then, does the `c` in `cfree()` stand? :-S – alk Aug 08 '15 at 07:39
  • It's in the same original source file (see *cremno*'s comment). So I feel `calloc()` and `cfree()` are definitly related. – alk Aug 08 '15 at 08:01
  • @alk Then maybe a naming convention to match `calloc` (in case `calloc` doesn't use `malloc` in its implementation)? – Theodoros Chatzigiannakis Aug 08 '15 at 08:05
  • If the main purpose of `calloc()` would have been to `clear` out memory, there simply would have been no need for another "freeing"-function. So to turn this around: The existence of `cfree()` can be taken as an evidence, that `calloc()` was thought for some kind of different memory handling then `alloc()`, different besides just `0`ing out the memory. – alk Aug 08 '15 at 08:06
  • @alk If a `calloc` implementation chose to allocate memory in a different way than `malloc` does, it could have a different book-keeping scheme to keep track of the chunks it gives out. In that case, having another counterpart "freeing" function would be reasonable (maybe even necessary). For the cases where `calloc` is implemented by calling `malloc`, `cfree` can just be an alias for `free`. Consider this, for example: http://stackoverflow.com/questions/2688466/why-mallocmemset-is-slower-than-calloc – Theodoros Chatzigiannakis Aug 08 '15 at 08:14
  • I feel you should give credits to *Anant Barthwal* as s/he brought up the suggestion of "contiguous" in this answer (http://stackoverflow.com/a/31889465/694576) 1st. – alk Aug 08 '15 at 09:18
  • @alk For the purposes of not overlapping with Anant's answer (which I've also upvoted, as I have your comment in the question), I've instead removed my mention of *contiguous* and rephrased my answer (to not say that these two are the "most" plausible etymologies). – Theodoros Chatzigiannakis Aug 08 '15 at 09:28
  • @cremno: `calloc()` already had been introduce by V6 (http://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/iolib/calloc.c) – alk Aug 09 '15 at 16:42
  • You might like to have look at my answer (http://stackoverflow.com/a/31906184/694576), if you do not mind. – alk Aug 09 '15 at 16:53
  • 4
    @alk: Oh! It's interesting that it doesn't clear the memory (at least explicitly). More interestingly it isn't the only function beginning with `c`. Maybe `c` just stands for C (the programming language) as `open` or `alloc` were already used. See Lesk's [The Portable C Library (on UNIX)](http://www.tom-yam.or.jp/2238/ref/iolib.pdf) which seems to be a document about this `iolib` (doesn't 100% match the V6 one though). – cremno Aug 09 '15 at 17:09
  • Another interesting variation I found is `gcalloc()` which in V8 implemented a "*garbage-compacting allocator*": http://man.cat-v.org/unix_8th/9/alloc It also had is own free function: `gcfree()` @cremno – alk Aug 09 '15 at 17:15
  • 3
    @cremno If you feel you have found enough clues for the "c" referring to the C language itself, may I suggest you make it an answer? (To me, it sounds like the most convincng etymology right now.) – Theodoros Chatzigiannakis Aug 09 '15 at 23:16
  • contiguous alloc maybe? – David Callanan Jul 21 '19 at 13:54
10

I did some research and found the following in "UNIX@ TIME-SHARING SYSTEM: UNIX PROGRAMMER'S MANUAL. Seventh Edition, Volume 2", chapter "PROGRAMMING" (Italics by me):

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.

  • The last sentence is a clear evidence that calloc() was definitely (meant to be?) more different from malloc() then just by clearing out the memory.

    Interesting enough there is no reference to free() on any of those some hundred pages ... :-)

  • Moreover UNIX V6 already had calloc() which calls alloc(). The (linked) source does not show any approach to zero out any memory.

Concluding from the both facts above I strongly object the theory that the leading "c" in calloc() stands for "clear".

alk
  • 69,737
  • 10
  • 105
  • 255
  • Maybe it originally stood for something else but it was definitely re-purposed (like the rest of iolib that became V7's libstdio). I guess it'll be difficult to get a definitive answer (however Mr. Lesk seems to be still alive). Sadly there's already an accepted answer. Btw. what about “count”? – cremno Aug 09 '15 at 17:26
  • @cremno: I won't speculate in an answer. In the moment, however I'd tend to bet on "*coalesce*" or "*compact*". – alk Aug 09 '15 at 17:30
  • 1
    I don't follow your conclusion about the last sentence of the documentation as being "clear evidence" of anything, except for the fact that it allows an implementation of `calloc` to do something different than `malloc` and zeroing (for example, giving memory from a pre-zeroed source). – Theodoros Chatzigiannakis Aug 09 '15 at 17:49
  • @TheodorosChatzigiannakis: Did you study the sources for `calloc()`'s V6 implemetation I linked? `free()` returns chunks to the pool with out clearing them. `calloc()` fetches from the pool without clearing anything. – alk Aug 09 '15 at 17:51
  • I am not a reputation-whxxe, I simply did some research, present my results and conclude, what makes sense. @TheodorosChatzigiannakis – alk Aug 09 '15 at 17:53
  • @TheodorosChatzigiannakis, and for the "clear evidence": `calloc()`'s V7 implementation calls `malloc()` and after `malloc()` returns `calloc()` clears out the memory. As it calls `malloc()` `free()` could be used, so no need for `cfree()`. But there was a `cfree()` in V7. Why introducing it if there is no need for it, as `free()` would do? – alk Aug 09 '15 at 18:00
  • The way I read it, it seems like the first bullet is a conclusion drawn from the documentation and not from the second bullet. Am I reading your answer correctly in this regard, or not? – Theodoros Chatzigiannakis Aug 09 '15 at 18:01
  • @TheodorosChatzigiannakis: You are right, my two findings are presented in the wrong order, for concluding from top to button. I'll be correcting this now ... – alk Aug 09 '15 at 18:04
  • As I said, `cfree` could be there to allow implementations to use a different book keeping scheme that could be incompatible with `malloc`. Future proofing that proved unnecessary, in other words. – Theodoros Chatzigiannakis Aug 09 '15 at 18:04
  • 1
    No need to edit your answer. I only wanted to read your opinion but what about c meaning C as it was originally part of the portable C library (see [this](http://www.tom-yam.or.jp/2238/ref/iolib.pdf) PDF I've linked earlier)? It also supports your first argument. – cremno Aug 09 '15 at 18:05
  • Makes sense then, I'm upvoting since your findings are likely to lead us to the correct answer. – Theodoros Chatzigiannakis Aug 09 '15 at 18:09
  • @cremno: With the background of the existence of the "*The Portable C Library (on UNIX)*" having been building in the same stable as UNIX during those days and the fact it introduced the naming convention starting function names with "c" the idea that the "c" in `calloc()` simply indicates those functions are "C" functions (as opposed to UNIX functions) might be a simple and plausible answer to the OP's question, yes, indeed ... :-) – alk Aug 09 '15 at 20:18
4

I don't think anybody knows. But describing the calloc() call with the semantics that the memory must be cleared, as opposed to malloc (memory allocate) which returns any random rubbish that was left over from a previous free() operation, is a useful modus operandi for students, which is useful in that it reminds the user that malloc() returns an unsafe value.

jrrk
  • 161
  • 3
3

calloc = contiguous memory allocation.

It means according to syntax of calloc() i.e

void *calloc (size_t number_of_blocks, size_t size_of_each_block_in_bytes);   

it receives two parameters: no. of blocks and size of one block, so it allocates an array of memory for the no. of blocks you will provide.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 14
    But `malloc` and `realloc` also allocate a contiguous region of memory. It would be very strange to name something after a property it shares with all other similar things. – David Richerby Aug 08 '15 at 10:06
  • 2
    Old topic but to me contiguous makes the most sense as it allocates n chunks of a given size contiguously, whereas `malloc` and `realloc` only allocate one chunk of a given size. You could read `malloc(x)` as "allocate x bytes" and `calloc(n,x)` as "allocate n chunks of x bytes contiguously" – Kwantuum Jun 12 '18 at 17:50
  • 1
    I was just thinking the same thing.. contiguous sounds right. – David Callanan Jul 21 '19 at 13:55
  • @DavidCallanan Not necessarily. "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." - Section 8.7 of The C Programming Language, K&R 2nd Ed. – Seraphendipity Aug 29 '20 at 01:25
0

As Anant's answer says, it stands for Contiguous Allocation. Per

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.

Section 8.7 of The C Programming Language, K&R, 2nd Ed.

  • 1
    the heap itself is not contiguous, but blocks of allocated memory separately are **always** contiguous, logically at least – Kaiyakha Dec 09 '20 at 22:53