hi .. can we design a program in c which will point out the possible
memory leaks in any given c file and fix them.... i am trying to come
with something like this but do not know where to start...
any help on where to begin would be highly appriciated
kind regards
rahul
well, any use of malloc() without a later free() is going to leak....
But I think it's ttheoretically impossible to figure out what mallocs()
never do a free() without executing all possible paths thru the code
with all possible inputs.
The best way is to write really structured code, something like this:
p = malloc( ... );
dosomethingwith(p);
free( p );
.... and don't have any other paths that can bypass the free().
In practice what's usually done is you write really crappy spaghetti
code, with the free()'s nowhere near the malloc()'s, then use a logging
malloc(), such as
www.dmalloc.com gives out, then you run your program,
with luck having it exercise all the possible code paths, then you look
at the list of still-allocated blocks.
The last time I did so, I forgot to free one important struct of
pointers so there were something like 36,337 unfreed pointers using
124,543,000 bytes. Ouch. You should really just write good code in
the first place.