free (NULL)
brnstnd at stealth.acf.nyu.edu
brnstnd at stealth.acf.nyu.edu
Wed May 30 10:24:19 AEST 1990
In article <3078 at goanna.cs.rmit.oz.au> ok at goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
[ on the general problem of how to tell if an object is no longer ]
[ being pointed to ]
> Can anyone suggest a better way of tackling this problem in portable C?
Your second solution does the job most of the time: keep a count next to
each pointer. With a disciplined style such as is enforced by, say, C++
or a good macro set, you'll never forget to deal with the count.
However, you do run into a problem with pointer loops: some number of
structures, each containing a pointer to the next one in a circle, and
none of which you actually need. There are solutions to this fundamental
problem, for which I refer you to Knuth's discussion of Lists.
Most of the time you don't need general List structures, and there's
some way to modify your design so that it's always clear when you can
free a pointer. One common technique is to do as little allocation as
possible in subroutines; whenever you do allocate something, you must
deallocate it in the same block. This fails only when a parent can't
predict how much memory a child will need, which is rare in practice.
---Dan
More information about the Comp.lang.c
mailing list