Translate

Sunday, 1 September 2019

malloc() vs calloc() – Difference Between malloc() and calloc() in C

Here in this tutorial you will learn about difference between malloc() and calloc() in C.

We all know that memory is available in limited size only, so it becomes important for us to use it efficiently. If we are writing a program that uses too much of memory resulting in wastage of memory or conversely if we allocate too little memory so that we can’t store enough information, that means we are not sure of the amount of data to be stored. In such cases, dynamic memory allocation comes to the rescue, where our program is capable to allocate whatever amount of memory it needs during the run-time of programs.

Now the question arises is that how can we achieve dynamic memory allocation in our programs?

In the C Language, we have predefined functions like calloc() and malloc() defined under the stdlib.h header file that can be used to allocate memory during the runtime of a program.

Let’s understand this awesomely useful function with simple examples:

malloc() Function

malloc() is an abbreviation for memory-allocation. It is a predefined function defined in the stdlib.h header file. It is used to allocate memory during the runtime of a program. The memory allocated is uninitialized that means it has garbage values.

Syntax:

ptr = (type*) malloc (size in bytes to be allocated)

The malloc() function takes size in bytes to be allocated as argument. If the memory allocation is successful, it returns a void pointer to the beginning of the allocated memory. This void pointer can be type-casted to any type of pointer. If the memory allocation fails due to reasons like insufficient memory, the malloc() function returns a NULL pointer.

It is the responsibility of the programmer to deallocate the allocated memory before program ends using free() or realloc() functions.

C Program Implementation of malloc() Function

#include <stdio.h>  
#include <stdlib.h>

int main()  
{
    /* The memory allocated by malloc contains garbage value */   
    /* allocates 10*sizeof(int) = 40 bytes of memory */  
    int *ptr1 = (int*)malloc( 10 * sizeof(int));      
    
    /* allocates 8*sizeof(char) = 8 bytes of memory  */  
    char *ptr2 = (char*)malloc( 8 * sizeof(char)); 
    
    if(ptr1)   
    { 
        printf("Memory allocation successful !!");  
        
    }  
    else  
    {  
        printf("Memory allocation failed !!");  
        
    }  
    
    free(ptr1);  
    free(ptr2);  
    return 0;  
}

Output

Memory allocation successful !!

calloc() Function

calloc() is an abbreviation for c-contiguous a-allocation. It is an advancement over the malloc() function. It is used to allocate multiple blocks of memory of the same size dynamically. The memory is initialized with zero.

Syntax:

ptr = (type*) calloc (number of blocks , the size of blocks in bytes to be allocated)

The calloc() function takes two arguments. First argument is the number of blocks of memory to be allocated and the second argument is used to define the size of blocks in terms of bytes. If the memory allocation is successful, it returns a void pointer to the beginning of the allocated memory. This void pointer can be type-casted to any type of pointer. If the memory allocation fails due to reasons like insufficient memory, the calloc() function returns a NULL pointer.

It is the responsibility of the programmer to deallocate the allocated memory before program ends using free() or realloc() functions.

C Program Implementation of calloc() Function

#include <stdio.h>  
#include <stdlib.h>  

int main()  
{  
    /* The calloc function initialize the memory with 0 */   
    /* allocates 4 blocks of memory with size of int */  
    int *ptr1 = (int*)calloc(4, sizeof(int));      
    
    /* allocates 8 blocks of memory with size of float */  
    float *ptr2 = (float*)calloc(8, sizeof(float));   
    
    if(ptr1)   
    {  
        printf("The values are:\n");  
        for(int i=0;i<4;i++)  
        {  
            /* printing the data of memory-allocated */  
            printf("Memory block %d : %d \n",i+1,*(ptr1+i));   
        }  
    }  
    else  
    {  
        printf("Memory allocation failed !!");  
    } 
    
    free(ptr1);  
    free(ptr2);  
    
    return 0;  
}

Output

The values are:
Memory block 1 : 0
Memory block 2 : 0
Memory block 3 : 0
Memory block 4 : 0

malloc() vs calloc() – Difference between malloc() and calloc()

Parameters malloc() calloc()
Abbreviated for m-memory,  alloc-allocation c-contiguous,  alloc-allocation
Syntax: (void*) malloc (n * size in bytes) (void*) calloc (n , size in bytes)
Definition It is a predefined function defined in stdlib.h header file used to allocate memory dynamically in terms of bytes. It is predefined function present in stdlib.h header file used to allocate memory dynamically in terms of number of blocks of memory with given size in bytes.
No.  of Arguments It takes single argument. It takes two arguments.
No.  of memory blocks Allocates a single block of memory with given byte-size. Allocates multiple blocks of memory that are contiguous in memory with given byte-size.
Initialization It does not initialize the allocated memory. It initializes the allocated memory blocks with 0.
Garbage value Present Absent
Speed It is fast. It takes time to allocate multiple blocks of memory.
Used for Creating structures Creating dynamic Arrays

Comment down below if you have any queries related to malloc() and calloc() in C.

The post malloc() vs calloc() – Difference Between malloc() and calloc() in C appeared first on The Crazy Programmer.



No comments:

Post a Comment