Calloc vs. Malloc

When calloc is used to allocate a block of memory, the allocated region is initialized to zeroes. In contrast, malloc does not touch the contents of the allocated block of memory, which means it contains garbage values. This could potentially be a security risk because the contents of memory are unpredictable and programming errors may result in a leak of these contents.

Comparison chart

Edit this comparison chart

Calloc

Malloc

Function allocates a region of memory large enough to hold "n elements" of "size" bytes each. Also initializes contents of memory to zeroes. allocates "size" bytes of memory.
Number of arguments 2 1
Syntax void *calloc (number_of_blocks, size_of_each_block_in_bytes); void *malloc (size_in_bytes);
Contents of allocated memory The allocated region is initialized to zero. The contents of allocated memory are not changed. i.e., the memory contains unpredictable or garbage values. This presents a risk.
Return value void pointer (void *). If the allocation succeeds, a pointer to the block of memory is returned. If the allocation of memory fails, a NULL pointer is returned. void pointer (void *). If the allocation succeeds, a pointer to the block of memory is returned. If the allocation of memory fails, a NULL pointer is returned.

Contents: Calloc vs Malloc

edit Syntax and Examples

edit malloc()

void *malloc(size_t size);

allocates size bytes of memory. If the allocation succeeds, a pointer to the allocated memory is returned. Otherwise NULL is returned. Example:

/* Allocate memory for an array with 15 elements of type int. */
int *ptr = malloc(15 * sizeof (int)); 
if (ptr == NULL) {
   /* Memory could not be allocated, so print an error and exit. */
   fprintf(stderr, "Couldn't allocate memory\n");
   exit(EXIT_FAILURE);
} 
/* Allocation succeeded. */

Note that malloc requires that we calculate the bytes of memory we need, and pass that as an argument to malloc.

edit calloc()

void *calloc(size_t nelements, size_t bytes);

allocates a contiguous block of memory large enough to hold nelements of size bytes each. The allocated region is initialized to zero. In the above example:

/* Allocate space for an array with 15 elements of type int and initialize to zeroes. */
int *ptr = calloc(15,sizeof (int));
if (ptr == NULL) {
   /* Memory could not be allocated, so print an error and exit. */
   fprintf(stderr, "Couldn't allocate memory\n");
   exit(EXIT_FAILURE);
} 
/* Allocation succeeded. */

calloc(m, n) is the same as

p = malloc(m * n); 
if(p) memset(p, 0, m * n);

edit Video explaining calloc, malloc and realloc

This video tutorial explains memory allocation functions malloc, calloc and realloc, as well as the memory de-allocation function free:

edit Security considerations

It is generally a good idea to use calloc over malloc. When you use malloc, the contents of the allocated memory are unpredictable. Programming errors may cause these memory contents to leak in unintended but highly vulnerable ways. A good example of such a leak is the Heartbleed vulnerability in OpenSSL, the basic mechanism of which is explained in this XKCD comic and some more technical details are in this blog post.

edit Speed of execution

calloc is a tiny bit slower than malloc because of the extra step of initializing the memory region allocated. However, in practice the difference in speed is very small and can be ignored.

edit References

Share this comparison:

If you read this far, you should follow us:

"Calloc vs Malloc." Diffen.com. Diffen LLC, n.d. Web. 17 Dec 2014. < >

Related Comparisons Follow Diffen
Make Diffen Smarter.

Log in to edit comparisons or create new comparisons in your area of expertise!

Sign up »
Top 5 Comparisons

Comments: Calloc vs Malloc

Comments via Facebook

Anonymous comments (10)

February 1, 2014, 5:52am

Thanks for the difference. That helped me!

— 115.✗.✗.148
2

August 31, 2013, 1:19pm

well explained

— 117.✗.✗.171
2

May 28, 2014, 4:58am

Thnx....

— 106.✗.✗.237
1

May 16, 2014, 6:28pm

Thanks a lot

— 103.✗.✗.197
1

July 3, 2013, 5:43am

nice explanation. :)

— 202.✗.✗.146
1

December 6, 2010, 10:05am

Prasanna - Good Explaination..!!!

— 90.✗.✗.42
1

August 25, 2010, 1:40pm

I appreciate the clarification!

— 152.✗.✗.33
1

January 17, 2014, 5:27pm

thnx...

— 122.✗.✗.251
0

September 16, 2013, 5:25pm

nice

— 122.✗.✗.68
0

March 15, 2013, 10:29am

nice clearification..................apreciated work .....good job.:)

— 1.✗.✗.116
-3

share

Up next

Git vs. Mercurial