(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.

Top 5 Future Inventions in Technology 2019 - 2050


There are lots of expected future inventions in technology.Among them, we have taken top five which are given below.The video below the list contains detail about them.Don't forget to watch and comment below which one is best.

The following numbers are in descending order :

5. Cicret Bracelet :


Smart Cicret Bracelet

4. I Watch :

I Watch in Future

3.Wall-Format Display Glass :

Future Touch screen

2.Smart Card :

Smart Card in Future

1.Smart Newspaper :

Smart newspaper in future

Watch the video of top 5 expected future Inventions in Technology Below:




Difference Between SRAM and DRAM




Random Access Memory(RAM)  is temporary memory of computer system.It is also known as volatile memory.It is divided into two types: SRAM and DRAM where SRAM stands for Static Random Access Memory and DRAM stands for Dynamic Random Access Memory.




The six differences between SRAM and DRAM are as follows:
  • SRAM is faster than DRAM.
  • SRAM is static but DRAM is dynamic.
  • SRAM consumes less power than DRAM.
  • SRAM is used as Cache Memory but DRAM is used as Main Memory.
  • DRAM is cheaper than SRAM.
  • DRAM uses less transistors per bit of memory than SRAM.