[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Possible memory leak in an exercise of a C handbook



On 16 Dec 2024 16:05 +0100, from martellif67@gmail.com (Franco Martelli):
> Is there a memory leak? What it sounds strange to me is that Valgrind
> reports: "total heap usage: 9 allocs, 8 frees, …" when for me the calls to
> "malloc" should be 8, not 9.

Put in something to count the number of calls to malloc() and free()
respectively. Don't forget calls outside of loops.

This can be as simple as a `printf("+");` and `printf("-");`
respectively at each call site. Or put breakpoints at each (or at the
respective function entry point) and keep a manual count.

See how many times each is reached.

-- 
Michael Kjörling
🔗 https://michael.kjorling.se


Reply to: