Javascript required
Skip to content Skip to sidebar Skip to footer

What Does the C in C4 Stand for

5 Answers 5

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.

answered Aug 8 '15 at 0:40

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.

    Aug 8 '15 at 0:43

  • @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.

    Aug 8 '15 at 0:55

  • And for what then, does the c in cfree() stand? :-S

    Aug 8 '15 at 7:39

  • @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).

    Aug 9 '15 at 17:09

  • @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.)

    Aug 9 '15 at 23:16

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

answered Aug 9 '15 at 16:09

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

    Aug 9 '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".

    Aug 9 '15 at 17:30

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

    Aug 9 '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.

    Aug 9 '15 at 17:51

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

    Aug 9 '15 at 18:05

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

67.4k 10 gold badges 84 silver badges 228 bronze badges

answered Aug 8 '15 at 3:52

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.

    Aug 8 '15 at 10:06

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

    Jun 12 '18 at 17:50

  • I was just thinking the same thing.. contiguous sounds right.

    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.

    Aug 29 '20 at 1:25

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.

answered Aug 8 '15 at 11:30

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.

answered Aug 29 '20 at 2:50

1

  • the heap itself is not contiguous, but blocks of allocated memory separately are always contiguous, logically at least

    Dec 9 '20 at 22:53

Not the answer you're looking for? Browse other questions tagged c malloc libc calloc c-standard-library or ask your own question.

What Does the C in C4 Stand for

Source: https://stackoverflow.com/questions/31888422/what-does-the-first-c-stand-for-in-calloc