What is the difference between doing:
ptr = (char **) malloc (MAXELEMS * sizeof(char *));
or:
ptr = (char **) calloc (MAXEL
Number of blocks:
malloc()
assigns single block of requested memory,
calloc()
assigns multiple blocks of the requested memory
Initialization:
malloc()
- doesn't clear and initialize the allocated memory.
calloc()
- initializes the allocated memory by zero.
Speed:
malloc()
is fast.
calloc()
is slower than malloc().
Arguments & Syntax:
malloc()
takes 1 argument:
bytes
calloc()
takes 2 arguments:
length
bytes
void *malloc(size_t bytes);
void *calloc(size_t length, size_t bytes);
Manner of memory Allocation:
The malloc
function assigns memory of the desired 'size' from the available heap.
The calloc
function assigns memory that is the size of what’s equal to ‘num *size’.
Meaning on name:
The name malloc
means "memory allocation".
The name calloc
means "contiguous allocation".