Real-world use case where calloc() is absolutely necessary over malloc()?
malloc - What does the first "c" stand for in "calloc"? - Stack Overflow
How Do I Know When to Use Malloc or Calloc in C Programming?
newbie question, malloc vs calloc
Videos
As a CS student, I'm trying to understand the practical trade-offs between calloc() and malloc(). I know calloc() zeroes the memory. But are there specific, real-world C applications where relying on mallOC() + manual zeroing would lead to subtle bugs or be technically incorrect? Trying to move past the textbook difference.
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:
- Count or counted, because
calloctakes a separate count argument. 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.cseems 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 filemalloc.c, the word clear appears again, in reference to the wordcalloc.
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.
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
numbytes. The pointer returned is sufficiently well aligned to be usable for any purpose.NULLis returned if no space is available.char *calloc(num, size);allocates space for
numitems each ofsizesize. The space is guaranteed to be set to 0 and the pointer is sufficiently well aligned to be usable for any purpose.NULLis 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 frommalloc()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 callsalloc(). 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".
I am a beginner in programming in general and I am taking a C programming class required for my major currently in the summer. Since it is in the summer, the class is sped up. We have a final exam coming up and we have to write programs out by hand. We will be given tasks like "create a program that does this...".
I know how to use malloc and calloc. I read and watched all the Youtube videos on it but something is still not clicking for me. I know what it does, it reallocates memory or it sets memory aside for me to use. However, I don't know WHEN to use it and that will be a problem if I am taking the final exam.
So if I am given problems on the exam, how do I identify when to use malloc or calloc? When do I use malloc or calloc? Does it matter which one I choose over the other? I know the subtle difference between them, but I just don't know when to use them....