Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 697
Raymond Hettinger Avatar asked Aug 08 '15 00:08

Raymond Hettinger


People also ask

What does the C in calloc () stand for?

C calloc() The name "calloc" stands for contiguous allocation. The malloc() function allocates memory and leaves the memory uninitialized, whereas the calloc() function allocates memory and initializes all bits to zero.

What is calloc short for?

Calloc stands for contiguous allocation. Malloc function is used to allocate a single block of memory space while the calloc function in C is used to allocate multiple blocks of memory space. Each block allocated by the calloc in C programming is of the same size.

What is use of calloc () function?

The calloc() function in C++ allocates a block of memory for an array of objects and initializes all its bits to zero. The calloc() function returns a pointer to the first byte of the allocated memory block if the allocation succeeds. If the size is zero, the value returned depends on the implementation of the library.

What is malloc () and calloc () in C?

malloc() function creates a single block of memory of a specific size. calloc() function assigns multiple blocks of memory to a single variable. 2. The number of arguments in malloc() is 1. The number of arguments in calloc() is 2.


2 Answers

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.
like image 171
Theodoros Chatzigiannakis Avatar answered Sep 19 '22 23:09

Theodoros Chatzigiannakis


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

like image 25
alk Avatar answered Sep 17 '22 23:09

alk