[LCP]garbage collector

David Spencer David.W.Spencer at oracle.com
Mon Nov 4 22:49:01 UTC 2002


A garbage collector encourages sloppy programming - it would be better 
on cleanup if the code made the programmer's mistake obvious: something 
like "You forgot to free X bytes that were allocated by %s in %s() at 
line %d".  I take two approaches to memory management - either the 
function that allocates the memory also frees it, or the memory 
management is farmed out to a set of manager functions that takes care 
of the low level memory stuff and frees everything on its termination.

Calling this code a garbage collector is a little inaccurate as a 
garbage collector is supposed to remove stuff you no longer have a 
reference to.  This code doesn't have a mechanism for determining which 
blocks are unreferenced.

Might be nice to know what the postfix is about and why it needs to be a 
pointer to a pointer in a struct rather than just a simple pointer 
without having to work it out from the code.  Code could generally do 
with a little more commenting for less experienced users to work out how 
to use it and what it's doing.  "It was hard to write so it should be 
hard to read" doesn't apply if you genuinely want other people to be 
able to use, and more importantly know they can rely upon, your code.

Presumably usage is expected to be something like
#define XMALLOC(n) xmalloc(n,__FILE__,__FUNC__,__LINE__)
(assuming __FUNC__ exists, which it might not, in which case this won't 
work)
then you can simply replace malloc(foo) with xmalloc(foo).

Dave.


mehul radheshyam choube wrote:

> here is the garbage collector code.
> please check the code.
>
> waiting for reply.
>
> mehul.






More information about the linuxCprogramming mailing list