What does the first "c" stand for in "calloc"?
Asked Answered
C

6

61

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?

Charente answered 8/8, 2015 at 0:33 Comment(1)
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().Frequent
G
63

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.
Gann answered 8/8, 2015 at 0:40 Comment(15)
The V7 (the version that added malloc() and calloc() to Unix) source code uses clear in a comment and actually clears the allocated memory block.Humfrey
@Humfrey 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.Gann
And for what then, does the c in cfree() stand? :-SSchaumberger
It's in the same original source file (see cremno's comment). So I feel calloc() and cfree() are definitly related.Schaumberger
@Schaumberger Then maybe a naming convention to match calloc (in case calloc doesn't use malloc in its implementation)?Gann
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 0ing out the memory.Schaumberger
@Schaumberger 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: #2688966Gann
I feel you should give credits to Anant Barthwal as s/he brought up the suggestion of "contiguous" in this answer (https://mcmap.net/q/24039/-what-does-the-first-quot-c-quot-stand-for-in-quot-calloc-quot) 1st.Schaumberger
@Schaumberger 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).Gann
@cremno: calloc() already had been introduce by V6 (minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/iolib/…)Schaumberger
You might like to have look at my answer (https://mcmap.net/q/24039/-what-does-the-first-quot-c-quot-stand-for-in-quot-calloc-quot), if you do not mind.Schaumberger
@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) which seems to be a document about this iolib (doesn't 100% match the V6 one though).Humfrey
Another interesting variation I found is gcalloc() which in V8 implemented a "garbage-compacting allocator": man.cat-v.org/unix_8th/9/alloc It also had is own free function: gcfree() @HumfreySchaumberger
@Humfrey 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.)Gann
contiguous alloc maybe?Crushing
S
9

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".

Schaumberger answered 9/8, 2015 at 16:9 Comment(12)
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”?Humfrey
@cremno: I won't speculate in an answer. In the moment, however I'd tend to bet on "coalesce" or "compact".Schaumberger
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).Gann
@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.Schaumberger
I am not a reputation-whxxe, I simply did some research, present my results and conclude, what makes sense. @TheodorosChatzigiannakisSchaumberger
@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?Schaumberger
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?Gann
@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 ...Schaumberger
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.Gann
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 PDF I've linked earlier)? It also supports your first argument.Humfrey
Makes sense then, I'm upvoting since your findings are likely to lead us to the correct answer.Gann
@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 ... :-)Schaumberger
P
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.

Pomcroy answered 8/8, 2015 at 11:30 Comment(0)
A
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.

Anurous answered 8/8, 2015 at 3:52 Comment(4)
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.Adit
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"Tagmeme
I was just thinking the same thing.. contiguous sounds right.Crushing
@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.Unsphere
U
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.

Unsphere answered 29/8, 2020 at 2:50 Comment(1)
the heap itself is not contiguous, but blocks of allocated memory separately are always contiguous, logically at leastSweetscented
M
0

what if you're misinterpreting the term 'clear'? It has two meanings; it deletes what was, or it makes something clear.

It would make sense if it meant making clear how much memory is to be allocated. Malloc means memory allocate, and it requires as parameter the number of bytes that are to be allocated. If calloc meant clear memory allocate, it would make sense, because it requires you to input how many elements you're allocating and how big those elements are in bytes.

Mcglone answered 8/4 at 17:56 Comment(1)
Welcome to Stack Overflow! Since the original question doesn't mention "clear", it appears you are attempting to reply to another answer here. This should typically be done in a comment, once you have enough reputation. As it is written, it doesn't make much sense, since we have no idea to which of the other answers you are referring to (there's no "standard" sort order). You should edit your answer to address the question, and if you want to reference another answer, please link to it. Thanks!Hawes

© 2022 - 2024 — McMap. All rights reserved.