Learning to detect memory leaks in C - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Learning to detect memory leaks in C

juniorcoderjuniorcoder FinlandPosts: 1Member

I have often a problem in programming that valgrind says the there can be some leaks. What kind of methods there are to make sure if my code leaks memory or is it some library not written by me which leaks memory?



  • Malcolm_McLeanMalcolm_McLean Posts: 254Member ✭✭

    It's unlikely that an established library is leaking memory for your particular program. Whilst you do get leaks and other bugs, usually they are only triggered when you do something very unusual. A new library could leak.

    valgrind will tell you where the allocation was made if you pass --leak_check=full. There's no one way of plugging leaks. Normally, allocations should either be followed by a matching free in the same function, or the allocated memory should be returned and freed by caller, or the allocation should be inserted into an elaborate structure which is then killed or freed in one go. C won't tell you which pointer "owns" the memory, and there's no convention. So you need to be clear in your own mind, which is the owner and which pointers are just links.

Sign In or Register to comment.