Monday, April 5, 2021

Dynamic Memory Allocation

 Dynamic Memory Allocation

The Dynamic Memory Allocation can be defined as a procedure in which the memory is allocated during the runtime.

 The C language provides some functions to achieve this tasks. These library functions provided by C are defined under <stdlib.h> header file.

The functions are:

1.         malloc()

2.         calloc()

3.         free()

 

The malloc() method:

“malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer which can be cast into a pointer of any form. It initializes each block with default garbage value.

                  Syntax:

ptr = (cast-type*) malloc(byte-size)

For Example:

ptr = (int*) malloc(10 * sizeof(int));

             As the size of int is 2 bytes, this statement will allocate 20 bytes of memory. And, the pointer ptr holds the address of the first byte i.e. base address in the allocated memory.

 Programe 01:

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

void main()

{

  int i,no,num;

  int *ptr;

  clrscr();

  printf("Enter number of elements to be store ");

  scanf("%d",&no);

  ptr = (int *)malloc(no * sizeof(int));

  for(i=0;i<no;i++)

  {

    printf("Enter %d number ",i+1);

    scanf("%d",&num);

    *ptr = num;

    ptr++;

  }

  for(i=0;i<no;i++)

  {

    ptr--;

    printf("\n Number is %d",*ptr);

 

  }

  getch();

}

Programe 02:

#include<stdio.h>

#include<conio.h>

main()

{

  int no,rd,i;

  int *ptr,*ptr2;

  clrscr();

  printf("Enter how many numbers you want to store.. ");

  scanf("%d",&no);

  ptr = (int *) malloc(no * sizeof(int));

  ptr2 = ptr;

  for(i=0;i<no;i++)

  {

     printf("Enter a number ");

     scanf("%d",&rd);

     *ptr = rd;

     ptr++;

  }

  for(i=0;i<no;i++)

  {

     printf("Numbers are %d\n",*ptr2);

     ptr2++;

  }

  getch();

}

Programe 03:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

main()

{

  int no,i;

  float num;

  float *ptr, *ptr1;

  clrscr();

  printf("How many number you want to read ");

  scanf("%d",&no);

  ptr = (float *) malloc(no * sizeof(float));

  ptr1 = ptr;

  for(i=0;i<no;i++)

  {

     printf("Enter numbers.. ");

     scanf("%f",&num);

     *ptr = num;

     ptr++;

  }

  for(i=0;i<no;i++)

  {

     printf("\n Number is %f",*ptr1);

     ptr1++;

  }

  getch();

}

Programe 04:

#include <stdio.h>

#include <stdlib.h>

main()

{

    int* ptr;

    int n, i;

 

    n = 5;

    printf("Enter number of elements: %d ", n);

 

    // Dynamically allocate memory using malloc()

    ptr = (int*)malloc(n * sizeof(int));

 

      for (i = 0; i < n; ++i)

       {

            ptr[i] = i + 1;

       }

  

        // Print the elements

        printf("The elements are: ");

        for (i = 0; i < n; ++i)

        {

            printf("%d, ", ptr[i]);

        }

    }

}

  

calloc() method:

The “calloc” or “contiguous allocation” function in C is used to dynamically allocate the specified number of blocks of memory of the specified type. It initializes each memory block with a default value ‘0’.

               Syntax:

ptr = (cast-type*)calloc(n, element-size);

              For Example:

ptr = (float*) calloc(25, sizeof(float));

 This statement allocates contiguous space in memory for 25 elements each with the size of the float. If space is insufficient, allocation fails and returns a NULL pointer.

 Example 01:

#include<stdio.h>

#include<conio.h>

main()

{

  int no,rd,i;

  int *ptr,*ptr2;

  clrscr();

  printf("Enter how many numbers you want to store.. ");

  scanf("%d",&no);

  ptr = (int *) calloc(no, sizeof(int));

  ptr2 = ptr;

  for(i=0;i<no;i++)

  {

     printf("Enter a number ");

     scanf("%d",&rd);

     *ptr = rd;

     ptr++;

  }

  for(i=0;i<no;i++)

  {

     printf("Numbers are %d\n",*ptr2);

     ptr2++;

  }

  getch();

}

 

Example 02:

#include <stdio.h>

#include <stdlib.h>

void main()

{

    int* ptr;

    int n, i;

   // Get the number of elements for the array

    n = 5;

    printf("Enter number of elements: %d", n);

   // Dynamically allocate memory using calloc()

    ptr = (int*)calloc(n, sizeof(int));

       // Get the elements of the array

        for (i = 0; i < n; i++)

       {

            ptr[i] = i + 1;

        }

       // Print the elements of the array

        printf("The elements are: ");

        for (i = 0; i < n; i++)

       {

            printf("%d, ", ptr[i]);

        }

    }

  }

 

 free() method:-

The “free” function in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated by their own. The function free() is used to remove the references/ memory allocated. It helps to reduce wastage of memory by freeing it.

 

Syntax:

free(ptr);

 

 

 Difference between malloc() and calloc() functions:

 

 

malloc()

calloc()

Malloc function contains garbage value.

The memory block allocated by a calloc function is always initialized to zero.

Number of arguments is 1.

Number of argument is 2.

Time efficiency is higher than calloc().

Time efficiency is lower than malloc()

Malloc() function returns only starting address and does not make it zero

Before allocating the address, Calloc() function returns the starting address and make it zero.

It does not perform initializes of memory.

It performs memory initialization.