C Dynamic Memory Allocation (DMA)

The process of allocating memory at run time is called dynamic memory allocation. C provides the function malloc() ,  calloc(), realloc() and free() in the standard library for dynamic memory  allocation, resizing and freeing and their function prototype are in stdlib.h.

Dynamic memory allocation functions and their description

malloc() – It takes a single argument of type size_t and  allocates the specified number of byte. After successful allocation of memory , it returns a pointer of type void * that points initial address of requested memory, If fails to allocate then it returns NULL pointer.

calloc() – It takes two argument. It allocates contiguous memory of the specified number of bytes and initialize them to zero. After successful allocation of memory , it returns a pointer of type void * that points initial address of requested memory, If fails to allocate then it returns NULL pointer.

Take a case where already allocated memory with malloc() or calloc() is not enough or more than required, This time realloc() come in picture.

realloc(void *ptr, size_t size) – It allocates a memory block of new size, where

ptr is pointer to a memory block already allocated with malloc() or calloc(). If this is NULL, a new block is allocated and a pointer to it is returned by the function. And size is the new size for the memory block, in bytes. If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned.

free() – Memory that has been dynamically allocated using malloc() or calloc() does not get returned to the system automatically upon function exit. So free() if used to free allocated memory and return to system. The programmer must use free()explicitly to return the space.

Programmer should must ensure for every malloc() or calloc() there should be one free() function.

Example program to show use  of malloc()

int main()

{

   char *str = NULL;

   /* Memory allocation using malloc() function */

   str = (char *) malloc(20);

    if (NULL == str)

   {

    printf(“Error in allocating memory\n”);

    exit (1);

    }

   strcpy(str, “gyantoday.com”);

   printf(“String = %s,  Address = %u\n”, str, str);

   free(str);

   return 0;

}

Output:

String = gyantoday.com, Address = 75429822 

Example program to show use  of calloc()

int main()

{

   int i, count;

   int *num;

   printf(“Number of elements to be entered:”);

   scanf(“%d”,&count);

   /* Memory allocation using calloc() function */

   num = (int*)calloc(count, sizeof(int));

   printf(“Enter %d numbers:\n”,count);

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

   {

      scanf(“%d”,&num[i]);

   }

   printf(“The numbers entered are: “);

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

   {

      printf(“%d “,num[i]);

   }

  free(str);

  return 0;

}

Output :

 Example program to show use  of realloc()

int main()

{

   char *str = NULL;

   /* Initial memory allocation using malloc() function*/

   str = (char *) malloc(5);

   if (NULL == str)

   {

    printf(“Error in allocating memory\n”);

    exit (1);

    }

   strcpy(str, “gyan”);

   printf(“String = %s,  Address = %u\n”, str, str);

   /* Reallocating memory using realloc() function */

   str = (char *) realloc(str, 15);

   strcat(str, “today”);

   printf(“String = %s,  Address = %u\n”, str, str);

   free(str);

   return 0;

}

Output:

String = gyan, Address = 75429822

String = gyantoday, Address = 75429822

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.