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 prior 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()
Comparison Chart
Basis of comparison | malloc() | calloc() |
---|---|---|
No of blocks | Assigns single block of demanded memory. | Assigns 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. | The allocated memory is initialized to zero by using calloc(). |
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. |
Speed | Fast | Comparatively slow. |
Definition of malloc()
The malloc function assigns 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 is not able to allocate the required amount of memory. The malloc function is accessible in TURBO C, header file alloc.h
or stdlib.h
, and on UNIX it will be available in the header file <malloc.h>
.
SYNTAX
The syntax of this function is as follows:
malloc(number of elements * size of each element);
for example,
int *ptr;
ptr=malloc(10*sizeof (int))
Where size represents the size of memory required in bytes(i.e., the number of contiguous memory locations to be allocated).
But as mentioned before that the function malloc returns a void pointer, consequently a cast operator is required to change the returned pointer type based on our need, the above declaration could be presented in the following form:
ptr_var=(type_cast* ) malloc (size)
Where ptr_var
is the name of the pointer that retains the starting address of allocated memory block, type_cast
is the data type into which the returned pointer (or type void) is to be transformed, and size describes the size of the allocated memory block in bytes.
For example:
int *ptr;
ptr=(int*) malloc (10 * size of (int));
Memory allocated by malloc function contains garbage value.
Note that to verify that if the corresponding request is generated by malloc to allocate the memory granted by system RAM, or rejected (in case if required space is not available). We can make use of the property in which the needed amount of memory is not assigned the malloc function returns a NULL.
Definition of calloc()
The calloc function operates precisely same as malloc function excluding the fact that it requires two arguments as in case of malloc() only one argument is needed.
For example:
int*ptr;
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 signify 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, following 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 function hold all zeros. The calloc function is also obtainable in the header file <stdlib.h>
or <alloc.h>
in TURBO C.
Key Differences Between malloc and calloc
The primary differences between malloc and calloc functions are:
- A single block of demanded memory is assigned in malloc while 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 item 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.
Conclusion
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 variable initialization is more important for you.
Leave a Reply