C has four library routines known as "memory management functions" that can be used for allocating and freeing memoiy during program execution. The functions are as follows :
Malloc : Allocates, request size of bytes & returns a pointer to the first byte of the allocated space.
Calloc : Allocates space for an array of elements, initializes them to zero & then returns a pointer to the memory.
Free : Free previously allocated space
Realloe : Modifies the size of previously allocated space.
The allocation functions allocates a block of contiguous bytes. The allocation can tail if tfte system does not have sufficient memory to satisfy the request. If it tails, it returns a NULL. We should therefore check whether the allocation is successful before using the memory pointer.
Malloc function is used to allocate a block of memory at run time. The malloc function reserves a block of memory of specified size and returns a pointer of type void. A void pointer can be aligned to array type of pointer. The syntax of malloc function is-
ptr = (data type*) malloc ( byte_size);
here ptr is a pointer of type data type. Malloc returns a pointer of data type to an area of data type to an area of memory with size byte_size.
int * x ;
x = (int*) malloc ( 5* sizeof (int));
On successful execution of above statement. 10 bytes (5x2 (size of int)) is reserved and the address of the first byte of the allocated memory is assigned to the pointer x.
Calloc is a memory allocation function used to allocate memory at run time. Calloc advises multiple blocks of storage, each of the same size & then sets all bytes to zero. The syntax of calloc is :
ptr = (data type *) calloc: (m, size);
The above statement allocates ccc.-.cjous space for n blocks, each of size esizc bytes. All bytes are initialized to zero and a pointer to fine first byte of the allocated region is returned.
x= (int *) calloc (5. 2);
On successful execution, it allocates 10 bytes of memory and initialize all bytes to zero and a pointer to the first byte of the allocate region assigned to the pointer x.
Compile-time storage of a variable is allocated and released by the system in accordance with its storage clan. In dynamic memory allocation, it is programmer's responsibility to release the space when it is not required. Releasing memory' is important when the memory is limited. We may release a block of memory, created by malloc or calloc function, if it is not required by using free function.
For example : free (x);
Where x is a pointer to a memory block Which has already been created by malloc or calloc function.
It is possible that the previously allocated memory is not sufficient and wo need additional space for more elements. It is also possible that the memory allocated is much larger than necessary and we want to reduce it. In both the cases, we can change the memory-size already allocated with the help of function realloc. This process is called the reallocation of memory. For example, if the original allocation is done by the statement.
x = (int*) malloc (10);
then reallocation of space may be done by the statement
x = (int*) realloc (x,5); /*size decrease*/
x = (int *) realloc (x.l5); /* Size increase*/
If real location is successful the old data is not lost but if there is not enough space for reallocation then the old contents of allocated memory is lost.