Difference between malloc and calloc

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.

Function calloc allocates a region of memory large enough to hold “n elements” of “size” bytes each. Also initializes contents of memory to zeroes. malloc allocates “size” bytes of memory.
Number of arguments calloc has 2 arguments malloc has 1 argument
Syntax void *calloc (number_of_blocks, size_of_each_block_in_bytes); void *malloc (size_in_bytes);
Contents of allocated memory Calloc allocated region is initialized to zero. Malloc 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.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s