The fundamental difference between malloc and calloc function is that calloc() needs two arguments instead of one argument which is required by malloc(). Both malloc() and calloc() are the functions which C programming language provides for dynamic memory allocation and de-allocation at run time.
Before understanding malloc() and calloc() functions first let us understand meaning of dynamic memory allocation. Memory allocation is the procedure of assigning the computer memory for the execution of programs and processes. We use dynamic allocation techniques, when it is not known in advance how much of memory space is needed for the program and process.
Dynamic memory allocation arises due to the problems associated with static memory allocation such as if fewer elements are stored, then the rest of the memory is unnecessarily wasted. Therefore, it overcomes the problems of static memory allocation where memory is allocated only when it is required.
Content: malloc() Vs calloc()
|Basis of comparison||malloc()||calloc()|
|No of blocks||Allocates an only single block of requested memory.||Allocates multiple blocks of the requested memory.|
|Syntax||void *malloc(size_t size);||void *calloc(size_t num, size_t size);|
|Initialization||malloc() doesn't clear and initialize the allocated memory.||calloc() initializes the allocated memory to zero.|
|Manner of Allocation||malloc() function allocates memory of size 'size' from the heap.||calloc() function allocates memory the size of which is equal to num *size.|
Definition of malloc()
The malloc function allocates a block of memory in bytes. The user should explicitly give the block size, it requires for the use.
Through malloc function program requests RAM of the system for allocation of the memory, if the request is conceded (i.e., the malloc function says successful in allocating memory), it returns a pointer to the first block of memory. It returns void type of pointer, that means any type of pointer can be assigned.
Although, it returns a NULL, if the malloc functions fail to allocate the required amount of memory. The malloc function is available in header file
stdlib.h in TURBO C, and on UNIX it will be available in the header file
The syntax of this function is as follows:
malloc(number of elements * size of each element);
Where size represents the size of memory required in bytes(i.e., the number of contiguous memory locations to be allocated).
But as already told that the function malloc returns a void pointer, so a cast operator is required to change the returned pointer type according to our need, the above declaration would take the following form:
ptr_var=(type_cast* ) malloc (size)
ptr_var is the name of the pointer that holds the starting address of allocated memory block,
type_cast is the data type into which the returned pointer (or type void) is to be converted, and size specifies the size of allocated memory block in bytes.
ptr=(int*) malloc (10 * size of (int));
Memory allocated by malloc function contains garbage value.
Note that to check that if the request made through the function malloc to allocate the memory granted by system RAM, or rejected (in case if required space is not available). We can make use the fact that if the necessary amount of memory is not allocated the malloc function returns a NULL.
Definition of calloc()
The calloc function works exactly similar to malloc function except for the fact that it needs two arguments as against one argument required by malloc().
ptr = (int*)calloc(10,2);
Here 2 specifies the size of the data type in a byte for which we want the allocation to be made, which is this case is 2 for integers. And 10 specify the number of elements for which allocation is to be made.
Remember that the argument passed to the function malloc was (n*10), it is a single argument don’t be confused because multiple arguments are always separated by commas. The argument (n*10) has no commas in between. Hence it is a single argument, though not a simple one but an expression.
Returning to the above declaration, after the execution of above statement a memory block of 20 bytes is allocated to the requesting program and the address of the first block is assigned to the requesting program, and the address of the first block is assigned to the pointer ptr.
Memory allocated by calloc funtion contains all zeros. The calloc funtion is also available in the header file
<alloc.h> in TURBO C.
Key Differences Between malloc and calloc
The primary differences between malloc and calloc functions are:
- malloc allocates an only single block of requested memory whereas multiple blocks of requested memory are allocated by calloc.
- The malloc function doesn’t clear and initializes the allocated memory. It contains garbage value and contents of the allocated memory can not be altered. In contrast, calloc initializes the allocated memory to zero.
- malloc is faster than calloc due to the requirement of additional steps of initialization in the calloc but the difference is negligible.
- Another difference between these two is that calloc is a malloc+memset, memset allocates the physical pages in memory whereas malloc only assigns the memory from the heap in the virtual address.
Both malloc and calloc functions are used for allocation of the memory and have their certain advantage, and disadvantage like malloc is fast as compared to calloc. Moreover, malloc easier to use as it takes only one argument because calloc allocates memory and initialize memory area with ZERO. But you would prefer to use calloc when you are more concern about variable initialization.