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?
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.
It looks like you're new here. If you want to get involved, click one of these buttons!