Mulugeta E. answered 04/12/19
Over 4 years of tutoring at College level
There are two noticeable differences between malloc and calloc functions.
- the number of arguments they take and what the arguments mean. each function are defined as follows
void *malloc( size_t n) -> this returns a pointer to n bytes of dynamically allocated memory.
void *calloc (size_t n, size_t size) -> this returns a pointer to enough free dynamically allocated
memory to n objects of the given size in the second parameter.
2.. calloc initializes all allocated memory to ZERO while malloc just returns the pointer. If the size you are allocating is big enough, you might see a slight speed difference because calloc takes the time to initialize each memory block.
Example of use:
int *ptr = (int *)malloc(10*sizeof(int)); //returns a pointer to enough space to hold 10 integers.
int *ptr = (int *)calloc(10, sizeof(int)); //returns a pointer to enough space to hold 10 integers, but the memory blocks are initialized to ZERO.
*in the above case the functionalities are the same.