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.
Syntax void *calloc (number_of_blocks, size_of_each_block_in_bytes); void *malloc (size_in_bytes);
Number of arguments 2 1
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. void pointer (void *). If the allocation succeeds, a pointer to the block of memory is returned.

edit Syntax and Examples

malloc()

void *malloc(size_t size);

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

/* Allocate space for an array with ten elements of type int. */
int *ptr = malloc(10 * 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()

void *calloc(size_t nelements, size_t bytes);

allocates a region 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 ten elements of type int. */
int *ptr = calloc(10,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. */


edit Related Information

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

edit Further Reading

There are some good books available on Amazon.com for further reading on C programming:

Comments: Calloc vs Malloc

Your IP address will be logged and parts of it shown publicly.
Write <youtube v=YouTubeVideoID /> to embed a YouTube video in your comment.
Comments with links (URLs) are not allowed because we get overrun by spam.

February 1, 2014, 5:52am

Thanks for the difference. That helped me!

— 115.✗.✗.148
0

January 17, 2014, 5:27pm

thnx...

— 122.✗.✗.251
0

September 16, 2013, 5:25pm

nice

— 122.✗.✗.68
0

August 31, 2013, 1:19pm

well explained

— 117.✗.✗.171
0

July 3, 2013, 5:43am

nice explanation. :)

— 202.✗.✗.146
0

March 15, 2013, 10:29am

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

— 1.✗.✗.116
0

December 6, 2010, 10:05am

Prasanna - Good Explaination..!!!

— 90.✗.✗.42
0

August 25, 2010, 1:40pm

I appreciate the clarification!

— 152.✗.✗.33
0
Stay informed 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 Recently Compared

share

Up next

Git vs. Mercurial