Sunday, 17 September 2017

Dynamic Memory Allocation In C

Today, we are talking about Dynamic memory allocation in c. Do you know what is Dynamic memory allocation and Static memory allocation?
First we will learn about Static memory allocation and Dynamic memory allocation by a brief definition of both.

Static Memory Allocation: Static memory allocation is the allocation of memory at compile time, before the associated program is executed. That is when we compile our program that time memory allocation is done in static memory allocation.

Dynamic Memory Allocation: Dynamic memory allocation where memory is allocated as required at runtime. It means that when we execute (after the compilation of program) our program then our memory allocation will be done.

There are mainly four function used in dynamic memory allocation :

  • malloc( )
  • calloc( )
  • free( )
  • realloc( )



malloc( ):

The prototype of malloc() function is given below:
     void * malloc(size_t); 

malloc() is a general purpose function which is used to dynamically allocate memory for any type of data at run time.  This function allocates the requested byte of memory and returns the address of the first byte of the allocated memory block.  Below is an example for dynamic memory allocation using malloc().

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

Size of an integer is 4 bytes.  So, we have requested 16 bytes(size of 4 integers) of memory. And malloc() function returns the address of the first byte of the allocated block. The type casting(int *) is used to convert the address returned by malloc() to the type "pointer to integer".

calloc( ):

The prototype of calloc() function is given below:
     void *calloc(size_t nmemb, size_t size);

calloc() is also a general purpose function which is used to dynamically allocate memory in multiple blocks of same size.  For calloc(), we need to provide two arguments.  One is number of elements and the other one is the size of each element.  This function also allocates the requested memory and returns the address of the first byte of the allocated block.

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

Here, number of elements(4) and size of integer(4 bytes) are passed as arguments.  So, calloc() function will allocate a 16 byte block and returns the address of the first byte of the allocated block.  Apart from that, calloc() writes 0 in all bytes of the allocated block. So, memory block allocated by calloc() won't have any junk values.  Whereas, the memory block allocated by malloc() will be uninitialized.


free( ):

The prototype for free() function is given below.
     void free(void *ptr);

free() function is used to deallocate the memory space allocated using malloc(), calloc() or realloc() function.


realloc( ):

The prototype for realloc() function is given below:
     void *realloc(void *ptr, size_t size);

realloc() function is used to modify the size of the memory block pointed by pointer ptr to size bytes.  The old contents in the existing memory block remains unchanged.  Whereas, the newly allocated memory region will be uninitialized.

     int *ptr = (int *)realloc(NULL, 40);

The above statement is equivalent to malloc(40).  In case if the size is 0 and pointer ptr is not NULL, then the call is equivalent to free(ptr);

     int *ptr = (int *)realloc(ptr, 0);


Examples of Static memory allocation and Dynamic memory allocation :


int arr[100]; //static memory allocation for an array of 100 elements.

int *ptr = (int *) malloc(100 * sizeof(int)); // dynamic memory allocation using malloc for 100 elements.

 int *ptr = (int *)calloc(100, sizeof(int)); // dynamic memory allocation using calloc for 100 elements.



Simple program for understanding the concept of Dynamic memory allocation:

 #include <stdio.h>
  #include <stdlib.h>

  int main() {
        int i, n, ch, *ptr_malloc, *ptr_callloc;

        printf("Enter number of inputs:");
        scanf("%d", &n);

        /* dynamic memory allocation using malloc, calloc */
        ptr_malloc = (int *) malloc(sizeof(int) * n);
        ptr_calloc = (int *) calloc(n, sizeof(int));

        for (i = 0; i < n; i++) {
                ptr_malloc[i] = i;
                ptr_calloc[i] = i;
        }

        printf("\nPrinting the values in ptr_malloc:\n");
        for (i = 0; i < n; i++) {
                printf("ptr_malloc[%d]: %d\n", i, ptr_malloc[i]);
        }

        printf("\nPrinting the values in cptr:\n");
        for (i = 0; i < n; i++) {
                printf("ptr_calloc[%d]: %d\n", i, ptr_calloc[i]);
        }

      
        free(ptr_malloc);
        free(ptr_calloc);
        return 0;
  }



No comments:

Post a Comment