Showing posts with label free. Show all posts
Showing posts with label free. Show all posts

Tuesday, December 24, 2013

How to deallocate dynamically allocate memory without using “free()” function.

NOTE: this is actually puzzle type question nobody  use it realloc() function to free the memory. But I heard, in many interviews this question had been asked.

Standard library function realloc() can be used to deallocate previously allocated memory. Below is function declaration of “realloc()” from “stdlib.h

void *realloc(void *ptr, size_t size);
 
If “size” is zero, then call to realloc is equivalent to “free(ptr)”. And if “ptr” is NULL and size is non-zero then call to realloc is equivalent to “malloc(size)”.

Let us check with simple example.

/* code with memory leak */
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    int *ptr = (int*)malloc(10);
 
    return 0;
}
 
Check the leak summary with valgrind tool. It shows memory leak of 10 bytes, which is highlighed in red colour.

 valgrind –leak-check=full ./free
  ==1238== LEAK SUMMARY:
  ==1238==    definitely lost: 10 bytes in 1 blocks.
  ==1238==      possibly lost: 0 bytes in 0 blocks.
  ==1238==    still reachable: 0 bytes in 0 blocks.
  ==1238==         suppressed: 0 bytes in 0 blocks.

Let us modify the above code.
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    int *ptr = (int*) malloc(10);
 
    /* we are calling realloc with size = 0 */
    realloc(ptr, 0);
    
 
    return 0;
}
 
Check the valgrind’s output. It shows no memory leaks are possible, highlighted in red color.
  >valgrind –leak-check=full ./a.out
  ==1435== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
  ==1435== malloc/free: in use at exit: 0 bytes in 0 blocks.
  ==1435== malloc/free: 1 allocs, 1 frees, 10 bytes allocated.
  ==1435== For counts of detected errors, rerun with: -v
  ==1435== All heap blocks were freed — no leaks are possible.