Reputation: 226624
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?
Upvotes: 61
Views: 12959
Reputation: 1
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.
Upvotes: 0
Reputation: 170
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.
Upvotes: 0
Reputation: 29233
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:
calloc
takes a separate count argument.Clear, because it ensures that the returned memory chunk has been cleared.
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
. C, as in the C language.
Upvotes: 64
Reputation: 70981
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 ofsize
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 fromcalloc
.
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".
Upvotes: 9
Reputation: 161
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.
Upvotes: 4
Reputation: 89
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.
Upvotes: 3