Showing posts with label DMA. Show all posts
Showing posts with label DMA. Show all posts

(DMA)Dynamic Memory Allocation in C - Explained with examples


The size of array you have declared initially can be sometimes insufficient and sometimes more than required.Dynamic memory allocation allows us to allocate additional more memory space or to release unwanted space at run time, thus optimizes the use of storage space.

There are 4 library functions under "stdlib.h" for dynamic memory allocation:
  1. malloc(); - Allocates requested size of bytes and returns a pointer to first byte of allocated space.
  2. calloc(); - Allocates space for an array elements, initializes to zero and then returns a pointer to memory.
  3. free(); - frees the previously allocated space.
  4. realloc(); - Change the size of previously allocated space.
1.malloc();
The name malloc stands for "memory allocation". The function malloc() reserves a block of memory of specified size and returns a pointer of type void which can be casted into pointer of any form.
Syntax of malloc();

ptr=(cast-type*)malloc(byte-size);
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.
 For example:
ptr=(int*)malloc(100*sizeof(int));
This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory.

2.calloc();

The name calloc stands for "contiguous allocation".The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.
 Syntax of calloc();

ptr=(cast-type*)calloc(n,element-size);
This statement will allocate contiguous space in memory for an array of n elements. 
For example:
 ptr=(float*)calloc(25,sizeof(float));
This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes.

3.free();

Dynamically allocated memory with either calloc() or malloc() does not get return on its own.The programmer must use free() explicitly to release space.
 syntax of free();

free(ptr);
This statement cause the space in memory pointed by ptr to be free. 

4.realloc();

If the previously allocated memory is insufficient or more than sufficient.Then, you can change memory size previously allocated using realloc().
 Syntax of realloc();

ptr=realloc(ptr,newsize);
Here, ptr is reallocated with size of newsize.

Have a look at following example to understand DMA in C which uses all four functions of DMA:

Q.Write a menu driven program to perform following operations in Array using DMA(Dynamic Memory Allocation):
1.Insert     4.Display
2.Search   5.Exit.
3.Delete
Solution:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    char *p,d,t;
    int s,i,a,c;
    printf("Enter 1D array size:\n");
    scanf("%d",&s);
    p=(char*)malloc(s*sizeof(char));
    printf("Enter array elements(char):\n");
    for(i=0;i<s;i++)
    {
       scanf("%s",&p[i]);
    }
    do
    {
       system("cls");
       printf("Choose option for Array Operation:\n1-Insert\n2-Search\n3-Delete\n4-Display\n5-Exit.\n");
       scanf("%d",&c);
       switch(c)
      {
       case 1:
       system("cls");
       printf("After which position you want to insert?\n");
       scanf("%d",&a);
       if(a>s-1)
      {
         printf("Invalid position!\a");
         break;
      }
      printf("What you want to insert?\n");
      scanf("%s",&d);
      p=(char*)realloc(p,s++);
      for(i=s-1;i>a+1;i--)
      {
        p[i]=p[i-1];
      }
      p[a+1]=d;
      printf("Your array elements after insertion are:\n");
      for(i=0;i<s;i++)
     {
        printf("%c ",p[i]);
     }
     break;
     case 2:
     system("cls");
     printf("What you want to search?\n");
     scanf("%s",&t);
     printf("Your element is at following location of array:\n");
     for(i=0;i<s;i++)
     {
        if(p[i]==t)
       {
          printf("%d,",i);
       }
      }
      break;
      case 3:
      system("cls");
      printf("Which position element you want to delete?\n");
      scanf("%d",&a);
      if(a>s-1)
     {
        printf("Invalid position!\a");
        break;
      }
      for(i=a;i<s-1;i++)
      {
         p[i]=p[i+1];
      }
      s--;
      p=(char*)realloc(p,s);
      printf("Your number is deleted!");
      break;
      case 4:
      system("cls");
      printf("The elements in array are:\n");
      for(i=0;i<s;i++)
     {
        printf("%c ",p[i]);
     }
     break;
     case 5:
      exit(0);
     default:
     printf("Your choice is wrong.");
     break;
   }
   getch();
   fflush(stdin);
   }while(1);

}
--------------------------------------------------------------------------------------------------------------------------
Don't forget to comment below if you have still any confusions.