Dynamic Memory Allocation

  • April 5, 2022
  • C
Matrix multiplication

In the C programming language, the concept of dynamic memory allocation allows the programmer to allocate memory at runtime. Four functions in the stdlib.h header file allows for dynamic memory allocation in the C language.

  • malloc()
  • calloc()
  • realloc()
  • free()
FunctionDescription
malloc() Allocates a single block of memory in response to a request.
calloc()Allocates multiple blocks of memory in response to a request.
realloc()reallocates memory that has been allocated by the malloc() or calloc() functions.
free()frees the memory that was allocated dynamically.

Note: the difference between static memory allocation and dynamic memory allocation.

static memory allocationdynamic memory allocation
Memory is allocated during the compilation process.At runtime, memory is allocated.
Memory can’t be increased while the program is running.Memory can be increased while the program is running.
used in the array.used in a linked list
malloc() function:

malloc() does not initialize memory at runtime, so it starts with a garbage value.

If memory is insufficient, it returns NULL.

The following is the syntax for the malloc() function:

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

Example:

#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));  //memory allocated using malloc    
    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 number of elements: 4
Enter elements of array: 12 4 9 10
Sum=35
calloc() function:

It starts by setting all bytes to zero.

If memory is insufficient, it returns NULL.
The calloc() function’s syntax is as follows:

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

Example:

#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));  //memory allocated using calloc    
    if(ptr==NULL)                         
    {    
        printf("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 number of elements: 3
Enter elements of array: 11 22 12
Sum=45
realloc() function:

If malloc() or calloc() run out of memory, you can use the realloc() function to reclaim it. In a nutshell, it alters the memory size.

Let’s look at the realloc() function’s syntax.

ptr=realloc(ptr, new-size)  
free() function:

The memory used by the malloc() and calloc() functions must be freed by using the free() function. Otherwise, memory will be consumed until the programme is terminated.

Let’s look at the free() function’s syntax.

free(ptr)  

Note: also read about the Pointer to Pointer in C

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *