James M. Lay
James M. Lay

Reputation: 2470

how to make sure all malloc'd memory has been freed?

I was wondering if there's a way to know the size of currently used malloc'd memory so I can compare before and after running tests to make sure all the memory has been freed. Something like this:

size_t nmembytes_before = malloc_bytes_allocated();
... do your tests ...
size_t nmembytes_after = malloc_bytes_allocated();
if (nmembytes_before != nmembytes_after)
{
    ... error reporting ...
}

Does such a thing exist, or are we relegated to using valgrind for all our memory profiling?

Upvotes: 0

Views: 76

Answers (1)

doron
doron

Reputation: 28892

Many heaps allow you to obtain statistics about allocations. Glibc offers: https://www.gnu.org/software/libc/manual/html_node/Statistics-of-Malloc.html#Statistics-of-Malloc

This should allow you to test if all bytes allocated have been freed

Upvotes: 1

Related Questions