Dynamic memory allocation
Most often we face situations in programming where the data is dynamic in nature. That is number of data items keeps changing during the execution of the program.
For example:
While declaring an array we are required to specify the size of the array. Specifying the size before the execution of the program may some times create memory related problem.
Suppose ,we declare an array for storing 10 elements.
At runtime we come to know that we are required to store only 5 elements in such a situation the extra five (5) locations created are wasted.
On the other hand if we want to store 20 elements then we do not have sufficient memory for the required elements.
In both the cases we have memory related problems either the memory is wasted or we do not have sufficient memory.
The above problems can be solved if we can specify and allocate the required amount of memory during execution of the program. Specifying and allocating memory during execution of the program is known as dynamic memory allocation.
“C” Language provide us with some functions known as dynamic memory allocation functions which help us to allocate and de-allocate memory during execution of the program. The functions provided by “C” language are:
malloc()
calloc()
realloc()
free()
header file related to the above function is
“stdlib.h” or “alloc.h”