Discuss the concept of Dynamic Memory Allocation (DMS)

Data Structure

Discuss the concept of Dynamic Memory Allocation (DMS)

We develope any program for processing the data, the data which to be processed that must be input into the computer. Before input of data we have to reserve space in primary memory. This reservation process is known as "Memory Allocation"

means in memory Allocation. We reserve space for keeping the data in primary memory.

Memory Allocation technique is divided into two types:-

  1. Static Memory Allocation
  2. Dynamic Memory Allocation

  1. Static Memory Allocation
  2. In this Allocation technique we reserve space during compile time of program, that space never expend or shrink during runtime of the program. So, Static Memory Allocation

is apply to that program in which we know total number of data in advance.

Eg:-Variable and arrays are used to perform Static Memory Allocation.

  • Dynamic Memory Allocation (DMA)
  • In this Allocation technique we reserve space during runtime of the program. The space reserved by (DMA) i.e

    Dynamic Memory Allocation technique. DMA technique can expend or shrink during runtime of the program. So, this Allocation technique is applied in that program in which we don't know size of data in advance. NOTE:-To perform DMA "C" provide concept of "Dynamic Array".
    • How to perform DMA:-

    For this purpose C provide four readymade function i.e define in malloc.h

    header file. These functions are as follow:-
    1. malloc( )
    2. calloc( )
    3. realloc( )
    4. free( )
    1. malloc( ):-

    It is used to perform Memory Allocation at runtime. It take 1 argument i.e totla number of byte to be reserve. After successfull alocation it return address of first byte that address is void type. So we need to typecast this address into particular type.

    Eg:-int n,*p; printf("Enter total number of data\n"); scanf("%d",&n); p=(int *)malloc(n * sizeof (int));

  • calloc( ):-
  • It is also used to reserve space at runtime of the program but there is a difference between malloc( )

    and calloc( ) i.e calloc( ) work on two argument and its space is initialized by zero.

    Eg:-int n,*p; printf("Enter total number of data\n"); scanf("%d",&n); p=(int *)calloc(n, sizeof (int));

  • realloc( ):-
  • It is used to add some memory block in previous created blocks either i.e created by malloc( ) or calloc( ). It works with two argument i.e "pointer to the previous created blocks"

    and total number of byte to the added.

    Eg:-int n,*p; printf("Enter total number of data\n"); scanf("%d",&n); p=(int *)calloc(n, sizeof (int)); printf("Enter total number of bytes to be added \n"); scanf("%d",&n); p=(int *)realloc(p,n* sizeof(int));

  • free( ):-
  • It is used to deallocate the allocated space. It take one argument i.e pointer of allocated blocks.

    Eg:-int n,*p; printf("Enter total number of data\n"); . . . p=(int *)realloc(p,n * sizeof(int)); free(p);

    • Discuss the concept of Data Structure its Operation and types:-
    To develope any program a programmer need to handle its component or any program have two component i.e instruction and data.
  • To handle both the component we have two different concept:-
    1. Concept of programming language
    2. Concept of Data structure
    1. Programming Language:-
    2. Programming Language is used to handle instruction of the program so it provide tools for writing instruction. E.g Tools for input and output data tools for putting condition.

    Post a Comment

    0 Comments