Sunday, February 21, 2010

Malloc and Free

A certain program makes use of malloc and free several times. Anyway, you forgot to free one block of memory allocated with one malloc. What strategy do you use to check the place where the unpaired malloc is located?


  1. If I have pointed all the free'd pointers to NULL and I know the name of all the pointers (say, I had declared them at the beginning of the program) then I would just check which one of them doesn't point to NULL.

    I am trying to improve my solution. Useful puzzle :)

  2. How about using tool like valgrind

    valgrind --tool=memcheck --leak-check=yes --show-reachable=yes