DYNAMIC MEMORY ALLOCATION


DYNAMIC MEMORY ALLOCATION:

We can allocate memory to variables in two ways:
  1. static memory allocation
  2. dynamic memory allocation


Allocation of memory at the time of compilation is called static memory allocation.

E.g., When an array is declared

int a[10];

It allocates 20 bytes of memory to store the integers but at runtime, we may or may not utilize it completely which results in wastage of memory. And moreover, it is also not possible to increase the size of memory during runtime. This disadvantage can be overcome by allocating the required memory at the time of execution. This allocating memory dynamically at the time of execution of program is called dynamic memory allocation. This can be done by standard functions (i) malloc and (ii) calloc.

(i)     malloc:- It is a pre-defined function used to allocate memory dynamically.

Syntax:-          Pointer variable=(datatype*)malloc(number of bytes required);
     
Here, the function malloc allocates memory for the byte size specified. It is converted to the pointer of datatype mentioned. It returns the address of first byte of the memory allocated which is assigned to the pointer variable. If the address allocation cannot be done, it returns NULL.
Eg.,            Ptr=(int*)malloc(10);

It allocates 10 bytes which stores integer values and the first byte address is stored in the pointer variable ptr.
Ø  A Program to implement malloc function.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
                  void main( )
{
int i,n,*p;
printf(“enter number of elements”);
scanf(“%d”,&n);
p=(int *)malloc(n*2);
printf(“enter elements”);
for(i=0;i<n;i++)
scanf(“%d”,p+i);
printf(“the elements are\n”);
for(i=0;i<n;i++)
printf(“%d\n”,p+i);
getch( );
}

(ii)   calloc:- This is also a pre-defined function for dynamic memory allocation which slightly differs from the function malloc. calloc function contains two arguments. The first one specifies number of data items for which the memory has to be allocated and second argument specifies the number of bytes for each data item.

Syntax:-
      Pointer variable=(datatype*)calloc(number of data items, size);
Eg.,
      Ptr=(int *)calloc(10,2);
The above example allocates memory for 10 data items, 2 bytes for each data item and the address of starting location is stored in the variable ptr.

Ø  A Program to implement calloc function.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
                  void main( )
{
float i,n,*p;
printf(“enter number of elements”);
scanf(“%f”,&n);
p=(int *)calloc(n,4);
printf(“enter elements”);
for(i=0;i<n;i++)
scanf(“%f”,p+i);
printf(“the elements are\n”);
for(i=0;i<n;i++)
printf(“%f\n”,p+i);
getch( );
}

(iii) realloc:- It is possible to increase or decrease already allocated memory by using this reallocation function.i.e., when once we allocate memory dynamically through malloc or calloc , the allocated block size can be increased or decreased by this function.

Syntax:-     Pointer variable=(datatype*)realloc(starting address of allocated memory, new 
                                                                                                                    byte size required);
Eg.,            Ptr=(int*)realloc(ptr,40);

Here, the first argument of realloc function specifies the starting address of already allocated block and the second argument specifies the new size required. The new byte size may be larger or smaller than already allocated size. When it is smaller, i.e., when we decrease the size, there is no change in the base address of newly allocated block where as when it is large, i.e., when we try to increase the size, then it first checks whether it is possible to extend the size without changing the base address. If it is not possible, it tries to allocate required memory somewhere else and after allocating the new block of memory, the contents are moved to new block and the new base address is stored in corresponding pointer variable.


Ø  A Program to implement realloc function.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
                  void main( )
{
int i,n,m,*p;
printf(“enter number of elements”);
scanf(“%d”,&n);
p=(int *)malloc(n*2);
printf(“enter elements”);
for(i=0;i<n;i++)
scanf(“%d”,p+i);
printf(“the elements and corresponding addresses are\n”);
for(i=0;i<n;i++)
printf(“%d\t%u\n”,*(p+i),p+i);
free(p);
printf(“enter new size”);
scanf(“%d”,&m);
p=(int*)realloc(p,2*m);
printf(“enter elements”);
for(i=0;i<m;i++)
scanf(“%d”,p+i);
printf(“after reallocation\n”);
printf(“the elements and corresponding addresses are\n”);
for(i=0;i<m;i++)
printf(“%d\t%u\n”,*(p+i),p+i);
getch( );
}

(iv)             free( ):-The memory once allocated can be deallocated using this function.

Syntax:-     free(starting address of allocated block);

Eg.,            free(ptr);








No comments:

Post a Comment