What is dynamic memory allocation?

Dynamic memory allocation is a method of allocating memory during run time. In dynamic memory allocation memory space can be created, modified, freed.

Dynamic memory is allocated on heap.

In c programming dynamic memory allocation and deallocation can be done using these 4 functions defined on stdlib.h header file.

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()

malloc() function in c

malloc() function allocates requested memory and returns void pointer to allocated memory. This void pointer can be casted to any pointer type.

malloc stands for memory allocation.

This function is defined in stdlib.h header file.

Prototype

void *malloc(size_t size);

Syntax

*pointer=(Cast_type*)malloc(no_of_bytes);

Parameter

malloc() takes size of the memory in bytes to be allocated.

Return value

malloc() function returns pointer to the memory allocated.

How to use malloc()?

Inorder to use malloc( ) function in your c program you have to include stdlib.h header file.

Call malloc() function by passing size of the required memory and assign the return value of malloc to a pointer variable.

After chack for null pointer, if pointer is null then there is some error for allocation of memory.

Below is a malloc() example program, try it yourself.

Example program

//C program to show how to use malloc in c
#include<stdio.h>
#include<stdlib.h>
int main()
{  
  int i,n,*pointer;
  printf("\n\n\tEnter the value of n\n\t");
  scanf("%d",&n);
  pointer=(int*)malloc(n*sizeof(int));
  if(pointer== NULL)
   printf("Memory cannot be allocated \n\t");
  else
   {
    printf("\n\tEnter the elements \n\t");
    for(i=0;i<n;++i){
      scanf("%d",(pointer+i));
      printf("\n\t");
    }
    printf("Elements are \n\t");
    for(i=0;i<n;i++)
     printf("%d \t",*(pointer+i));
    }
    printf("\n\n\t");
    free(pointer);
   return 0;
}
output for malloc function in c programming

Code explanation

First we need include stdlib.h header file to use malloc function. We declared a pointer, size of input elements. Then we request user to enter the size of an array. We allocate block of memory by using malloc function. Then we request user to enter the element. Those values are stored in the block of memory pointed by the pointer. Stored values are displayed on console.


calloc() function in c

calloc() function dynamically allocates specified number of blocks of memory and initializes each block to zero.

calloc stands for contiguous allocation.

This function is defined in stdlib.h header file.

Prototype

void *calloc(size_t items,size_t size);

Syntax

*pointer=(Cast_type*)calloc(num_of_blocks,element_size);

Parameters

calloc() function takes number of blocks to be allocated and the size of the elements as input parameters.

Return value

This function returns pointer to the allocated block of memory or NULL if it fails to do so.

How to use calloc?

Inorder to use calloc( ) function in your c program you have to include stdlib.h header file.

Call calloc() function by passing no of elements size of element and assign the return value of calloc to a pointer variable.

After chack for null pointer, if pointer is null then there is some error for allocation of memory.

Below is a calloc example program, try it yourself.

Example program

//C program to show how to use calloc in c
#include<stdio.h>
#include<stdlib.h>
int main()
{
  int i,n,*pointer;
  printf("\n\n\tEnter the value of n \n\t");
  scanf("%d",&n);
  pointer=(int*)calloc(n,sizeof(int));
  printf("\n\tEnter the numbers \n\t");
  for(i=0;i<n;i++){
   scanf("%d",&pointer[i]);
  }
  printf("\tNumbers are \n\t");
  for(i=0;i<n;i++){
   printf("%d \t",pointer[i]);
  }
  printf("\n\n");
  free(pointer);
  return 0;
}
output for calloc function in c programming

Code explanation

First we have to include stdlib.h header file in order to use calloc function.

We have declared a pointer, loop variable and size variable. Then we ask user to enter value of n. Afterwards we allocate the n block of memory by using calloc function.

Then we ask user to enter the values and print those values on console.

As we dynamically allocated space so it is better to use free function to free up those block of memory.


free() function in c

free() function deallocates the memory which is allocated by malloc(), calloc() or realloc() functions.

C programming doesnot have grabage collecting feature hence memory allocated by malloc(), calloc(), realloc() are not freed automatically.

If memory allocated is not freed then it may cause memory leakages, heap memory may become full. To achieve freeing allocated memory free() function is used.

This function is defined in stdlib.h header file.

Prototype

void free(void *pointer);

Syntax

free(pointer);

Parameter

This function takes pointer which points to the memory allocated by malloc, calloc or realloc functions as input parameter.

Return value

This function does not returns any value.

How to use free?

Inorder to use free( ) function in your c program you have to include stdlib.h header file.

Call free() function by passing the pointer for which you want to free memory.

Try the example program which demonstrates how to use free().

Example program

//C program to show how to use free in c
#include<stdio.h>
#include<stdlib.h>
int main()
{
  int i,n,*pointer;
  printf("\n\tEnter the value of n \n\t");
  scanf("%d",&n);
  pointer=(int*)calloc(n,sizeof(int));
  printf("\tEnter the numbers \n\t");
  for(i=0;i<n;i++)
   scanf("%d",&pointer[i]);
  printf("\tThe entered values are \n\t");
  for(i=0;i<n;i++)
  printf("%d\t",pointer[i]);
  printf("\n\n\t");
  free(pointer);
  return 0;
}
output for free function in c programming

Code explanation

First we have to include stdlib.h header file in order to support calloc function and free function.

We have declared a pointer, loop variable and size variable. Then we ask user to enter value of n. Afterwards we allocate the n block of memory by using calloc function.

Then we ask user to enter the values and print those values on console.

As we dynamically allocate spaces it is better to use free function to free up those block of memory.


realloc function in c

realloc() function is used to resize the block of memory which is dynamically allocated by malloc or calloc functions.

If previously allocated memory is insufficient, to change the memory size for required size realloc() function can be used.

realloc() dynamically reallocates block of memory. Using this function may change the memory addresses.

This function is defined in stdlib.h header file. It deallocates previously allocated memory and points to a new object.

Prototype

void *realloc(void *ptr, size_t size);

Syntax

pointer = realloc(pointer, new_size);

Parameters

This function takes pointer which is pointing to the memory block previously allocated by malloc or calloc and size of a new block of memory as input parameters.

Return value

This function returns the pointer pointing to a new object.

How to use realloc?

Inorder to use realloc() function in your c program you have to include stdlib.h header file.

Call realloc() function by passing pointer and new size of element and assign the return value of calloc to a pointer variable.

After chack for null pointer, if pointer is null then there is some error for allocation of memory.

Below is a realloc example program, try it yourself.

Example program

//C program to show how to use realloc in c
#include<stdio.h>
#include<stdlib.h>
int main()
{
  int n=3,i,*pointer,mult=1;
  pointer=(int*)calloc(n,sizeof(int));
  if(pointer==NULL)
  {
    printf("Memory cannot be allocated \n");
    exit(0);
  }
  printf("\nEnter the elements \n");
  for(i=0;i<n;++i)
  {
   scanf("%d",&pointer[i]);
   mult=mult*(pointer[i]);
  }
  printf("Result is %d \n",mult);
  pointer=(int*)realloc(pointer,5);
  printf("Enter the new elements \n");
  for(i=0;i<n;++i)
  {
   scanf("%d",pointer+i);
   mult*=*(pointer+i);
  }
  printf("Result is %d \n",mult);
  printf("\n\n");
  return 0;
} 
output for realloc function in c programming

Code explanation

First we need to include stdlib.h header file in order to use calloc and realloc functions.

Then we declare a pointer, variable n, loop variable i. Initially we allocate n block of memory and ask user to enter the elements. Then we multiply all the elements in an array. We print this result on console. Then we increase the size of an array by 5 using realloc function and ask user to enter the elements. We then print the new value on console.


People also ask for

What is malloc in c?

It is a c library function which allocates requested memory. It is used to change the size of an data structure during run time i.e. it dynamically allocates memory.

What is calloc in c?

calloc function dynamically allocates specified number of blocks of memory and initializes each block to zero.

What does calloc stands for?

calloc stands for contiguous allocation.

What is free in c?

free function deallocates the memory which is allocated by malloc, calloc or realloc.

What is realloc in c?

realloc function is used to resize the block of memory which is dynamically allocated by malloc or calloc functions.

Does realloc free the old block?

No. It copies the old block data to new block of memory.

Is realloc slow?

Yes it is. Each time we allocate new memory it copies old block elements with new one.

What is meant by dynamic memory allocation?

Dynamic memory allocation is a procedure where the memory is being allocated during runtime.

Where is dynamic memory allocated?

Dynamic memory is allocated on the heap.

Why malloc is faster than Calloc?

Malloc is faster than calloc because calloc takes time to initialize to zero and malloc doesnt initialize memory to any value.

Why is malloc used?

Malloc is used to dynamically allocate memory.


People also read