Dynamic Memory Allocation

  • Why Dynamic memory Allocation(DMA)?

  • Consider a declaration of array of size 10,int a[10]
  • Now as we have declare an array of size 10,so this declaration will reserve 20 bytes for array a in the memory.As array is of int type each element will need 4 bytes so total becomes to 40 Bytes.

  • Till now there is no problem with  ordinary way,But what if there was an need of only 5 elements , this makes wastage of memory

  • What if , we want to store more than 10 elements.

  • Other Reason of Using Dynamic memory Allocation is that we can increase the size at the runtime.

  • There are some memory allocations functions with the help of them you can solve above problems.


malloc() function






  • Allocates required size of bytes and returns pointer to the first byte.
  • let's understand by examples

  • Syntax : malloc() function
    (cast_type *)  malloc(bytes);
    
    If cast type is not specified,than malloc() function returns void pointer.

    Let's use malloc() function to allocate memory to integer pointer
    #include<stdio.h>
    void main()
    {
    int *p;
    
    p = (int*)malloc(sizeof(int));
     // malloc() function will return integer pointer.
    
    
    }
    
  • sizeof()function gives the bytes acquired by the variable ,data type in the memory
  • allocating memory to float pointer
    #include<stdio.h>
    void main()
    {
    float *p;
    
    p = (float*)malloc(sizeof(float));
     // malloc() function will return float pointer.
    
    
    }
    

    allocating memory to char pointer
    #include<stdio.h>
    void main()
    {
    char *p;
    
    p = (float*)malloc(sizeof(char));
     // malloc() function will return float pointer.
    
    
    }
    

    Now we will create array by allocating memory dynamically by malloc()

    allocating memory for 10 blocks of int type
    #include<stdio.h>
    void main()
    {
    int *p;
    p = (int*)malloc(sizeof(int)*10);
    //it will allocate 40 bytes
    //it will return pointer to first byte.
    
    }
    

  • Q:create a array of 10 students for storing rollno ,take rollno from the user.Create array dynamically
  • Solution:
    #include<stdio.h>
    void main()
    {
    int i;
    int *p;
    p = (int*)malloc(sizeof(int)*10);
    
    
    for(i=0;i < 10;i++)
     {  
        printf("Enter the student roll no:");
        scanf("%d",p+i);
        
     }
    }
    
  • As p is a pointer which has address of first element , so at every iteration we are adding i value to get address of next block.
  • At every iteration we are storing value at p+i address which is address of next block



  • Q:Create a array of float type of 10 elements ,and store the temperature of past 10 days and print them on the screen.
  • Solution:
    #include<stdio.h>
    void main()
    {
    int i;
    float *p;
    p = (float*)malloc(sizeof(float)*10);
    
    
    for(i=0;i < 10;i++)
     {   printf("Enter the Temperature:");
        scanf("%f",p+i);
     }
    printf("\ntemperature of past 10 days was: ");
    for(i=0;i<10;i++)
     {
        printf("%f\t",*(p+i));
     }
    }
    





  • Q:Create an array of int type of 10 elements.take the elements from the user and only print the even number.Create array dynamically.
  • Solution:
    #include<stdio.h>
    void main()
    {
    int i;
    int *p;
    p = (int*)malloc(sizeof(int)*10);
    
    
    for(i=0;i < 10;i++)
     {
        printf("\nEnter the Character:");
        scanf("%d",(p+i));
    
     }
    printf("\nEven number are : ");
    for(i=0;i < 10;i++)
     {  if(*(p+i) %2 == 0)
        {
         printf("%d\t",*(p+i));
        }
     }
    }
    






  • This how we can dynamically allocate memory by using malloc() functions.