Static and Dynamic Memory Allocation

In this article we will learn static and dynamic memory allocation process in detail in which we will cover following topics which are listed below..

  • Static memory allocation
  • Disadvantage of static memory allocation
  • Dynamic memory allocation
  • Types of Dynamic memory allocation
Static and Dynamic Memory Allocation

Static Memory Allocation

Those memory which are allocated at compile time are know as static memory allocation, it should be very clear the memory allocated during compilation time, or the time when the programmer is creating program is called static memory.
And the memory allocated is fixed it cannot be increased and decreased during run the program.
For Example:

int main()
{
  int arr[5]= {1,2,3,4,5};
  return 0;
}

here we can see that we have an array which consist of five elements although you can see over here the size is fixed and we cannot increase and decrease this size at run time.

Disadvantage of Static Memory Allocation

  • If you are allocating memory for an array during compile time when time then you have to fix the size of at the time of declaration.
  • If the value stored by the user in the array at run time is less than the size specified then there will be wastage of memory.
  • If the value storeby the array at run time is more than size specified than obviously the program may crash or misbehave, so all these problems is sorted by Dynamic memory allocation.

Dynamic Memory Allocation

The process of allocating memory at time of execution is called dynamic memory allocation,in other term we can say that user can allocate memory at run time.
  • All the dynamic memory allocation takes place in the heap segment of the memory, heap is an area of memory where memory is allocated or deallocated without any order or randomly.
  • There are certain built-in function( malloc( ),calloc( ),realloc( ),free( ) ) that can help in allocating or deallocating some memory at run time in heap.
  • Pointer play an important role in dynamic memory allocation because allocated memory can be accessed through pointers only.

1- malloc( ):

malloc( ) is built in function which are define in two header file <stdlib.h>,<alloc.h> malloc( ) is short name of " memory allocation " and is used to dynamically allocate single large block of contiguous memory according to the size specified.

Syntax:- (void*)malloc(sizeof(size));

malloc( ) function simply allocate a memory block according to the size specified in the heap and on success it return a pointer pointing to the first byte of the allocated memory else return NULL.

For Example:
#include<stdio.h>
#include<conio.h>
int main()
{
     int i,n;
int *ptr;
     printf("\n Enter the number of integers: ");
     scanf("%d",&n;);
     int *ptr=(int*)malloc(n*sizeof(int));
     if(ptr==NULL)
  {
       printf("Memory Not Available:");
       exit(1);
  }
     for(i=0;i<n;i++)
{
      printf("Enter an integer:");
      scanf("%d",ptr+1);
}
      for(i=0;i<n;i++)
  {
      printf("%d",*(ptr+1));
      return 0;
  }
}

2- Calloc( ):

Like malloc( ) function, calloc( ) function is also used to allocate multiple blocks of memory in dynamic ways it used two arguments instead of just one, because first arguments represent the number of blocks and the second arguments represent the size of each blocks.

It returns NULL value on failure.

Syntax: void *calloc(size_t n,size_t size);

For Example:
#include<stdio.h>
#include<conio.h>
int main()
{
     int i,n,block;
int *ptr;
     printf("\n Enter the number of integers: ");
     scanf("%d",&n;);
printf("\n Enter the no of Block Required:");
scanf("%d",&block);
     int *ptr=(int*)calloc(block,sizeof(int));
     if(ptr==NULL)
 {
       printf("Memory Not Available:");
       exit(1);
 }
     for(i=0;i<n;i++)
{
      printf("Enter an integer:");
      scanf("%d",ptr+1);
}
      for(i=0;i<n;i++)
 {
      printf("%d",*(ptr+1));
      return 0;
 }
}

3-realloc( )

realloc( ) is also a built in function which are define in <stdlib.h> and <alloc.h>, this function is used to change the size of memory block without losing the old data.

Syntax: void *realloc(void *ptr,size_t newsize);

it requires two arguments the first arguments is the pointer to the previously allocated memory and the second arguments is the new size.

It also return NULL value on Failure.

Important points related to realloc( )

  • This function moves the contents of the old block to a new block and the data of the old block is not lost.
  • We may lost the data when the new size is smaller than the old size
For Example: 
#include<stdio.h>
#include<conio.h>
int main()
{
     int i;
     int *ptr=(int*)malloc(2*sizeof(int));
     if(ptr==NULL)
     {
       printf("Memory is Not Alloacted");
       exit(1);
     }
       printf("Enter The two number:");
       for(i=0;i<2;i++)
       {
         scanf("%d",ptr+i);
       }
       ptr=(int*)realloc(ptr,4*sizeof(int));
       if(ptr==NULL)
       {
             printf("Memory is not available:");
             exit(1);
       }
       printf("Enter 2 More Integers:");
       for(i=2;i<4;i++)
       scanf("%d",ptr+i);
       for(i=0;i<4;i++)
       printf("%d",*(ptr+i));
       getch();
}

4-free( ):

free( ) is also a built in function which are define in <stdio.h> and <alloc.h>, this function is basically used to release the memory from the heap which are allocated dynamically by using malloc( ), calloc( ) and realloc( ).

Syntax: free(*ptr);

for Example:
#include<stdio.h>
#include<conio.h>
int *input()
{
     int i,*ptr;
     ptr=(int*)malloc(sizeof(int));
     printf("Enter the five numbers:");
     for(i=0;i<5;i++);
     scanf("%d",ptr+i);
     return ptr;
}
int main()
{
      int i,sum=0;
      int *ptr=input();
      for(i=0;i<5;i++)
      sum += *(ptr+i);
      printf("Sum is:%d",sum);
      free(ptr);
      ptr=NULL;
      return 0;
}

FAQ

Que-1 What is a static memory allocation ?

Ans-  Static memory allocation is nothing but it is type of way to allocate memory at compilation time.

Que-2 What is a dynamic memory allocation ?

Ans- Dynamic memory allocation is type of way to allocate memory dynamically(at run time) in computer memory.

Que-3 What is the header file of malloc( ) function ?

Ans- malloc( ) function is define in <alloc.h> and <stdlib.h>.

Que-4 What is the header file of calloc( ) function ?

Ans- calloc( ) function is define in <alloc.h> and <stdlib.h>.

Que-5 What is the header file of realloc( ) function ?

Ans- realloc( ) function is define in <alloc.h> and <stdlib.h>.

Que-6 What is the header file of free( ) function ?

Ans- free( ) function is define in <alloc.h> and <stdlib.h.

Post a Comment (0)
Previous Post Next Post