LinuxN00b LinuxN00b - 5 months ago 24
C Question

How to detect memory leaks as a new C programmer

I'm starting to learn C coming from a Java background. My first toy program is a binary search tree. I have implemented add, find, min, max, depthfirst and delete. My question stems from the delete method.

In order to debug I print the tree ( depth first ) to the console, then I remove nodes using delete and print the bst again. If it were Java I'd call it a day and move on but I'm worried that somewhere I forgot to free some memory ( or thought I did but free'd the wrong pointer or something ).

As a novice C programmer what is the best way to detect these sorts of memory leaks? I'm using CLion as my IDE if there are tools I should be using in the IDE but I would prefer some tricks that aren't IDE dependant.


I understand your concern, I remember an undergrad project that executed just fine in my home desktop, but was producing a memory leak in the TA's laptop. I couldn't even imagine at that point...

So after that I started to use Valgrind to check for memory leaks. If Valgrind said that we were cool, I would say we're cool and call it a day!

Pro tip: How do I use valgrind to find memory leaks?