I have just learned about the C calloc()
function the other day. Having read its description and how it differs from malloc
(1, 2), I get the idea that, as a non-embedded programmer, I should always use calloc()
. But is that really the case?
One reservation I have is the extra delay for accessing the calloc()
-ed memory, but I also wonder if there are cases when switching from malloc()
to calloc()
will break the program in some more serious way.
P. S. The zero-initializing aspect of calloc()
is quite clear to me. What I'm interested in learning about is the other difference between calloc()
and malloc()
- lazy memory allocation provided by calloc()
. Please don't post an answer if you're going to focus purely on the memory initialization aspect.
malloc() has high time efficiency. calloc() has low time efficiency. 5. The memory block allocated by malloc() has a garbage value.
If you need the dynamically allocated memory to be zero-initialized then use calloc . If you don't need the dynamically allocated memory to be zero-initialized, then use malloc . You don't always need zero-initialized memory; if you don't need the memory zero-initialized, don't pay the cost of initializing it.
The fundamental difference that exists between malloc and calloc in C language pertains to calloc() requiring two arguments instead of a single argument as needed by malloc(). Both calloc and malloc in C are essential functions of the middle-level programming language.
This is really a situation-dependent decision. Rule of thumb is
If you're first writing into the allocated memory, malloc()
is better (less possible overhead).
Example: Consider the following scenario
char * pointer = NULL;
//allocation
strcpy(pointer, source);
here, allocation can be very well using malloc()
.
If there's a possibility of read-before-write with the allocated memory, go for calloc()
, as it initializes memory. This way you can avoid the problem with unitialized memory read-before-write scenario which invokes undefined behavior.
Example:
char * pointer = NULL;
//allocation
strcat(pointer, source);
Here, strcat()
needs the first argument to be a string already, and using malloc()
to allocate cannot guarantee that. As calloc()
zero-initializes the memory, it will serve the purpose here and thus, calloc()
is the way to go for this case.
To elaborate the second scenario, quoting from C11
, chapter §7.24.3.1 (follow my emphasis)
The
strcat()
function appends a copy of the string pointed to bys2
(including the terminating null character) to the end of the string pointed to bys1
. The initial character ofs2
overwrites the null character at the end ofs1
. [....]
So, in this case, the destination pointer should be a pointer to a string. Allocating via calloc()
guarantees that while allocating using malloc()
cannot guarantee that, as we know, from chapter §7.22.3.4
The
malloc
function allocates space for an object whose size is specified bysize
and whose value is indeterminate.
EDIT:
One possible scenario where malloc()
is advised over calloc()
, is writing test stubs used for unit / integration testing. In that case, use of calloc()
can hide potential bugs which arrive with cases similar to the later one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With