Most often in our programs we face the situations where the data we input are dynamic in nature. The data that we input at a time might get changed at the other time, so the memory space allocated to store such data is sometime more or insufficient the other time. Hence it would really be better if we had certain techniques that would allow us to allocate the required memory spaces automatically at run time. For the user’s convenience C provides the facility to handle such situations more easily and effectively by using what is known as 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. Many languages permit a programmer to specify an array size at run time. Such languages have the ability to calculate and assign, during execution, the memory space required by the variables in a program. The process of allocating memory at the run time is called 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. They are listed below:
|malloc()||Allocates requested size of bytes and returns a pointer to the first byte of the allocated space.|
|calloc()||Allocates the 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.|
Malloc ( ):-
A block of memory be allocated using the function malloc(). The malloc() function reserves a block of memory of specified size and returns a pointer of type void. This means that, we can assign it to any type of pointer. The syntax of the malloc() function is:
Where ptr is pointer of datatype cast type and bytesize is total area of memory that is to be occupied by the pointer ptr.
int *tms, n;
scanf(“%d”,&n); total size
Here this statement reserves the n*sizeof(int) bytes for the pointer tms.
Calloc ( ):-
Calloc() is another memory allocation function that is normally used for requisting memory space at e\run time for storing derived data type such as arrays and structures. While malloc() allocated a single block of storage space, calloc() stores multiple blocks of storage, each of the same size and then sets all bytes to zero. The syntax of this function is:
The above statement allocated\s contiguous space for n blocks each of size elem_size bytes. All the bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space, a NULL pointer is returned.
struct tms *bishnu;
Free ( ):
Compile time storage of a variable is allocated and released by the system in accordance with its storage class with the dynamic run time allocation; it is our responsibility to release the space when it is not required. The release of storage space becomes important when the storage is limited.
When we no longer need the data we stored in a block of memory, and we do not intend to use that block of memory for future use, using the library function free() whose syntax is free(ptr); where ptr is a pointer ot a memory block which has been already created by malloc() or calloc(). Use of an invalid pointer in the call may create problems and cause the system crash.
In some cases the previously allocated memory is not sufficient and we need additional space for more elements. It is also possible that the memory allocated is much larger than the necessary and we want to reduce it. In both the cases, we can change the memory size already allocated with the help of library function realloc(). This process is called the reallocation memory. For example if the original allocation is done by the statement:
Then reallocation of space may be done by the statement
This function allocated a new memory space of size newsize to the pointer variable ptr and returns a pointer to the first byte of the new memory block. The new size may be larger or smaller than the size. Remember, the new memory block may or may not begin at the same place as old one. In case, it is not able to find additional space in the same region, it will create the same in an entirely new region and move the contents of the old block into the new block. The function guarantees that the old data will remain intact.