what is dynamic memory allocation in C, explain malloc, calloc, free, realloc
Dynamic memory allocation:
Most often we face situations in programming where the data is dynamic in nature.
That is the number of data items keep changing during execution of the program.
For example, consider a program for processing the list of customers of a corporation. The list grows when we need to allocate more memory space to the list to accommodate additional data items.
Such situations can be handled more easily and effectively by using dynamic data structures in conjunction with dynamic memory management techniques.
Dynamic data structures provide flexibility in adding, deleting or rearranging data items at run time. Dynamic memory management techniques permit us to allocate additional memory space or to release unwanted space at run time thus optimizing the use of storage space.
The process of allocating memory at run time is known as dynamic memory allocation.
Although C does not inherently have this facility, there are four library routines known as memory management functions that can be used for allocating and freeing memory during program execution.
These functions help us build complex application programs that use the available memory intelligently.
malloc
Allocates request size of bytes and returns a pointer to the
first byte of the allocated space.
calloc
Allocates space for an array of elements, initializes them to
zero and then returns a pointer to the memory.
free
Frees previously allocated space.
Realloc
Modifies the size of previously allocated space.