Memory is allocated to the respective data variables (of any type of data structure) in two manners that are dynamic and static.
The memory if it allocated to the variables declared, during the compilation process is called as a static memory allocation whereas the allocated to the variables, during the execution of the program is called as dynamic memory allocation.
Once the memory is allocated statically it cannot be altered during the execution of the program. So the programmer may need the memory that is to be allocated as per the request during the program execution. So there comes dynamic memory allocation. Dynamic memory allocation is possible only when pointers are used. A pointer variable is used to store the base address of dynamically allocated memory.
. . . . .
In this case when the program is compiled, main memory is allocated to the array. So it is static memory allocation. Once the memory is allocated to “array”, it cannot be altered during execution. Here if the programmer wishes to store more numbers of elements in the array it is not possible. He can change the size of array, only before compilation. Now if he is decides the size of array as 50 and if he is stored only few elements then there is wastage of main memory. Usually if the elements that are to be stored in an array is not decided before compilation and it is decided during the execution of the program then the static memory allocation has a major drawback.
In the order to overcome the above-mentioned problem now programmer can request the required memory during the execution of program using pointer variable. It is dynamic memory allocation.
For example, consider the following program segment:-
int *arr, size;
/* user can get size of the array during execution */
arr= (int *) malloc (size of (int) * size);
. . . .
In this case the dynamic memory is allocated by the malloc function during the execution of program and the base is returned is stored in a pointer variable arr. With the help of arr now it is possible to refer as many elements as user wishes during the execution of the program. That is the major concept of dynamic memory allocation.