Skip to content

malloc vs calloc

Reading Time: < 1 minute

malloc vs calloc differences provided here:

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.

Malloc:

1.It is used for memory allocation.

2.malloc() takes one argument that is,total amount of bytes,

syntax:void  *malloc(size_t n);

where  n is the total bytes of memory.

3.It is quicker  than calloc.

4.It returns a void pointer which point to first byte of allocated space.

 

Calloc:

1.It is used for contiguous memory allocation.

2.calloc() take two arguments those are:total  number of blocks and size of each block.

void  *calloc(size_t n, size_t size);

where n is total elements that holds size bytes each.

3.It takes longer time than malloc due to intialization of allocated memory by zero.

4.Calloc allocates space for array elements.

5.calloc returns void pointer to memory

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

 

 

 

See also  BigQuery an Introduction
Tags:

Leave a Reply