free (NULL)

Stephen Clamage steve at taumet.COM
Wed May 30 01:58:02 AEST 1990


In article <3078 at goanna.cs.rmit.oz.au> ok at goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>I have a data structure containing
>some pointers, and *I* have filled some of them in
>and the caller preset some of them to defaults.  Now I'm going to change
>one of them.  Should I free it? ...
>Can anyone suggest a better way of tackling this problem in portable C?

This is easy to handle (safely and portably) in C++ (which is not
precisely the question you asked).

In the constructor for the data structure, you set a private boolean that
says whether a the data pointed-to needs to be freed.  The destructor,
and each member function which modifies the structure, checks the
boolean to see whether to free the data.  These functions are written
once while the design is fresh in your mind.  Forever after, only
the member functions are used to modify the data, and no user of the
data type ever needs to think about freeing the data.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list