BLOG DETAILS

Dynamic Memory Allocation (DMA) in C

  • By Suresh Kulahry
  • |
  • March 29, 2019

For the execution of a program, it is essential to bring the program into the main memory. When a program does not fit into the main memory, parts of it are brought into the main memory one by one and the full program is executed eventually.

There are two techniques used for storing the information of C programs in the "main memory". The first technique involves storing global and local variables, which are fixed during compilation and remain constant throughout the run time of the program. Space for the local variable is allocated from the STACK each time when the variable comes into existence. Also known as Static Memory Allocation (SMA).

The second technique by which a program can obtain memory space in the main memory is with dynamic allocation. Also known as Dynamic Memory Allocation (DMA).

The method of allocation of memory at the time of execution of a program is called dynamic memory allocation.

Dynamic Memory Allocation

  • Dynamic memory allocation makes efficient use of memory by allocating the required amount of memory whenever needed.
  • In this method, the space for the program is allocated from the free space during the execution of the program. The free region of the memory is called the heap.

DYNAMIC MEMORY ALLOCATION FUNCTIONS:

  1. The malloc() function.
  2. The calloc() function.
  3. The realloc() function.
  4. The free() function.
The prototypes of these functions are declared in alloc.h and stdlib.h header files.
The variables or memory blocks which are created dynamically, they do not have any name only address is used for processing.

The malloc() Function:

The malloc() function is used to allocate memory space in bytes to the variable of different data types. The function reserves bytes of determined size and returns the base address to the pointer variable. The return type of malloc() function is a void pointer.

Syntax:

Pointer_Variable = (Data_Type *) malloc (Block Size);

Example:

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

In C language when we does not convert the void pointer address (which are return by malloc() function) into required pointer then compiler simply displayed a warning message “Non Portable Pointer Conversion” but in C++ it is mandatory to convert otherwise compiler flash an error message. So, it is good practice we always convert the void pointer address into required pointer data type.

The calloc() Function:

The calloc() function is useful for allocating multiple blocks of memory. This function is usually used for allocating memory for array and structure.

Syntax:

Pointer_Variable = (Data_Type *) calloc (Part1, Part2);

Where,

            Part1 = Number of Blocks.
            Part2 = Size of Each Block.

Example:

               int *ptr;
               ptr = (int*)calloc(5*sizeof(int));

The free() Function:

The free() function is used to release the memory if not required. Thus, using this function the wastage of memory is prevented.

Syntax:

free(Pointer_Name);

Programming Examples

void main()
{
        int *ptr,num,i=0,sum=0;
        char name[25];
        float percentage;
        clrscr();
        printf("Enter Name of Student: ");
        gets(name);
        printf("How Many Subject Student Offered: ");
        scanf("%d",&num);
        ptr=(int*)malloc(sizeof(int)*num);
        if(ptr == NULL)
        {
                printf("Memory Allocation Failed");
                getch();
                exit(0);
        }
        while(i !=num)
        {
                printf("Enter %d Subject Marks : ",i+1);
                scanf("%d",(ptr+i));
                sum = sum + *(ptr+i);
                i++;
        }
        percentage = sum/num;
        printf(" Name : %s",name);
        printf(" Sum of Marks : %d",sum);
        printf(" Percentage : %f",percentage);
        getch();
}