Dynamic Memory Allocation

Dynamic memory is allocated from the heap using some standard library functions. Two keys are used in dynamic memory functions are malloc() and free(). Dynamic memory allocation are enables the C programmer to allocate memory at runtime. In C language dynamic memory allocation is possible by four functions of stdlib.h header file.

  • malloc()
  • calloc()
  • realloc()
  • free()

Look at the methods by using in dynamic memory allocation

malloc() allocates single block of requested memory.
calloc() allocates multiple block of requested memory
realloc()reallocates the memory occupied by malloc() or calloc() function
free()free the dynamically allocated memory.

malloc() function in C

  • In malloc() function doesn’t initialize memory at execution time so it has garbage value initially.
  • malloc() function allocates single block of requested memory.
  • If memory is not sufficient then it returns NULL value.

Syntax of malloc() function is given below

ptr=(cast-type*)malloc(byte-size)

Let’s see the example of malloc() function

#include<stdio.h>  
#include<stdlib.h>  
int main(){  
  int n,i,*ptr,sum=0;    
    printf("Enter number of elements: ");    
    scanf("%d",&n);    
    ptr=(int*)malloc(n*sizeof(int));  
    if(ptr==NULL)                         
    {    
        printf("Sorry! unable to allocate memory");    
        exit(0);    
    }    
    printf("Enter elements of array: ");    
    for(i=0;i<n;++i)    
    {    
        scanf("%d",ptr+i);    
        sum+=*(ptr+i);    
    }    
    printf("Sum=%d",sum);    
    free(ptr);     
return 0;  
}

Calloc() function in C

  • calloc() function initially initalize all bytes to zero.
  • In C calloc() function allocates multiple block of requested memory.
  • It returns NULL value if memory is not sufficient.

Syntax of calloc() function is given below

ptr=(cast-type*)calloc(number,byte-size)

Let’s see the example of calloc() function

#include<stdio.h>  
#include<stdlib.h>  
int main(){  
 int n,i,*ptr,sum=0;    
    printf("Enter number of elements: ");    
    scanf("%d",&n);    
    ptr=(int*)calloc(n,sizeof(int));
    if(ptr==NULL)                         
    {    
        printf("Sorry! unable to allocate memory");    
        exit(0);    
    }    
    printf("Enter elements of array: ");    
    for(i=0;i<n;++i)    
    {    
        scanf("%d",ptr+i);    
        sum+=*(ptr+i);    
    }    
    printf("Sum=%d",sum);    
    free(ptr);    
return 0;  
}

Output

Enter elements of array: 3
Enter elements of array: 10
10
10
Sum=30

realloc()function in C

  • In realloc() function it changes the memory size.
  • Memory is not sufficient for malloc() or calloc() you can reallocate the memory by realloc() function.
  • realloc() function of memory maintains the already present value and new blocks will be initialized with default garbage value.

Syntax of the realloc() function is given below

ptr=realloc(ptr, new-size)

Let’s see the example of realloc() function is given below

#include <stdio.h> 
#include <stdlib.h> 
  
int main() 
{ 
    int* ptr; 
    int n, i;  
    n = 5; 
    printf("Enter number of elements: %d\n", n); 
    ptr = (int*)calloc(n, sizeof(int)); 
    if (ptr == NULL) { 
        printf("Memory not allocated.\n"); 
        exit(0); 
    } 
    else { 
  
        printf("Memory successfully allocated using calloc.\n"); 
        for (i = 0; i < n; ++i) { 
            ptr[i] = i + 1; 
        } 
        printf("The elements of the array are: "); 
        for (i = 0; i < n; ++i) { 
            printf("%d, ", ptr[i]); 
        } 
        n = 10; 
        printf("\n\nEnter the new size of the array: %d\n", n);
        ptr = realloc(ptr, n * sizeof(int)); 
        printf("Memory successfully re-allocated using realloc.\n"); 
  
        for (i = 5; i < n; ++i) { 
            ptr[i] = i + 1; 
        }  
        printf("The elements of the array are: "); 
        for (i = 0; i < n; ++i) { 
            printf("%d, ", ptr[i]); 
        } 
  
        free(ptr); 
    } 
  
    return 0; 
}

Output

Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5, 

Enter the new size of the array: 10
Memory successfully re-allocated using realloc.
The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

free() function in C

  • Memory occupied by malloc() or calloc() functions must be released by calling free() function.
  • In free() function it will consume memory until program exit.
  • It helps to reduce wastage of memory.

Syntax of free() function is given below

free(ptr);

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews