The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur.
The realloc () function is used to allocate memory and has the following prototype:
void * realloc (void * ptr, unsigned int num);
The function modifies the size of the previously allocated memory pointed to by *ptr to that specified by a. The value of a may be higher or lower than the original. A pointer to the block is returned because realloc () may need to move the block to increase its size. If this occurs, the old block content is copied into the new block, and no information is lost. If ptr is null, allocates size bytes and returns a pointer; if size is zero, the memory pointed to by ptr is released. If there is not enough memory to allocate, a null pointer is returned and the original block is left unchanged.
To avoid waste of memory or the memory leak (memory leaks), then we should do the reallocation of the spaces memory previously allocated by function malloc (), calloc () or realloc (). In the C language, this process will be carried out by using function free () which has the form of a pointer parameter. Here is the prototype of function free ().
void *free(void *p);
p must be a pointer here previously allocated using function malloc (), calloc () or realloc (). Here is an example of use function free ().
The use of pointers will usually cause memory leaks, the event where there is room memory is wasted in vain because the memory space can no longer in access to allocated. This memory leak can result in programs or our operating system be damaged or have a hang. Here is an example of the syntax which will result in a memory leak.
#include <stdio.h>
#include<conio.h>
int main (void)
{
/* Declare a pointer P which would point to the type of data int */
void *P;
int x = 10;
double y = 15.3;
clrscr();
/* Ordered memory space to put the type int */
P = (int *) malloc (sizeof (int));
/* Ordered pointer P to point to the address of variable x */
P = & x;
/* Display the value contained in the pointer P */
printf ("The value of P \t =% p \n", P);
printf ("Value *P \t =% d \n \n", * ((int *) P));
/* Ordered memory space to put the type double */
P = (double *) malloc (sizeof (double));
/* Ordered pointer P to point to the address of variable y */
P = & y;
/* Display the value contained in the pointer P */
printf ("The value of P \t =% p \n", P);
printf ("Value *P \t =% .1f \n", * ((double *) P));
return 0;
}
At first glancethe above programasproperlyandifimplementedwouldalsoprovide the following result.
In the above program, initially we booked a space (memory address) to placing a value of type int (ie the address FFF2) then record it into a pointer P. After the process of the pointer, in we booked a room on back to hold the value of type double (ie FFEA address) and record it back to the pointer P. This resulted in pointer P who was appointed address FFF2 move to point to FFEA address, so the address is no longer accessible FFF2 will result in the waste of memory space is wasted. This kind of incident called with a memory leak.
To avoid this, we should free FFF2 address before ordering pointer P to point to the new address, namely by using the function free (). Thus, the syntax of the program should write as follows.
#include <stdio.h>
#include<conio.h>
int main (void)
{
void * P;
int x = 10;
double y = 15.3;
clrscr();
P = (int *) malloc (sizeof (int));
P = & x;
printf ("The value of P \t =% p \n", P);
printf ("Value *P \t =% d \n \n", * ((int *) P));
/* Free pointer P with the function free () */
free (P);
P = (double *) malloc (sizeof (double));
P = & y;
printf ("The value of P \t =% p \n", P);
printf ("Value *P \t =% .1f \n", * ((double *) P));
return 0;
}