Home >>C Tutorial >Dynamic memory allocation in C

Dynamic memory allocation in C

Dynamic memory allocation in C

The C programmer is able to allocate the memory at runtime because of the conviction of the dynamic memory allocation in the C programming language. The four functions of the stdlib.h header file in the C language is responsible for the dynamic memory allocation in the C language,
Here are those four functions:

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

It is recommended that you should understand the difference between dynamic memory allocation and the static memory allocation in the C language in order to understand the functions that are mentioned above.

Static memory allocation Dynamic memory allocation
In this system, at the compiling time the memory is allocated. In this system, at the runtime the memory is allocated.
In this method, while the program is being executed, the memory cannot be increased. In this method, while the program is being executed, the memory can definitely be increased.
This method is widely used in the arrays in C language. This method is widely used in the linked list in the C language.

Now that we have learned the difference between these two, let’s understand those 4 functions or methods:

free() :The memory that is dynamically allocated is freed by this method.

calloc() :Multiple blocks of the requested memory are allocated by this method.

malloc() :A single block of the requested memory is allocated by this method.

realloc() :This method generally reallocates the memory that is being occupied by the malloc() or calloc() functions.

1. free() function in the C language

Some of the memory is occupied by the malloc() or calloc() functions in the C language and that memory is meant to be released. Hence, by calling the free() function that occupied memory is released. If the memory will not be freed then the memory will be consumed until the program is exited.

Here is the syntax of the free() function:

free(ptr) 

2. calloc() function in the C language

The multiple block of requested memory are allocated by the calloc() function. Initially, all the bytes are initialized to zero. If the memory is found to be not sufficient then this function returns the NULL value.

Here is the syntax of the calloc() function:

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

Here is the Example

#include  
#include  
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("Sorry! unable to allocate memory");    
        exit(0);    
    }    
    printf("Enter elements of array: ");    
    for(i=0;i
Output : Enter elements of array: 3 Enter elements of array: 10 10 10 Sum=30

3. malloc() function in the C language

A single block of the requested memory is generally allocated by the malloc() function in the C language. It has a value of the garbage at the initial level as at the time of the execution this function doesn’t initialize memory. In case the memory is not sufficient then it will return the NULL value.

Here is the syntax of the malloc() function:
ptr=(cast-type*)malloc(byte-size)
Here is the example

#include  
#include  
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
Output : Enter elements of array: 3 Enter elements of array: 10 10 10 Sum=30

4. realloc() function in the C language

The memory can be reallocated by the realloc() function in the C language provided that the memory is not sufficient for the malloc() or calloc() function. In simple words, this function can change the memory size.

Here is the syntax of the realloc() function in the C language:
ptr=realloc(ptr, new-size)

No Sidebar ads