This is the mail archive of the
mailing list for the glibc project.
Re: malloc problem/question in 2.1.3
Ronald de Man wrote:
> On Tue, Jun 06, 2000 at 01:42:02AM -0400, Mike Corbeil wrote:
> > Kaz Kylheku wrote:
> Nobody said that setting the pointer to NULL is absolutely necessary.
That's all the input that had been given. Not mentioning that this isn't
always necessary therefore made this more or less an absolute statement.
Otherwise, one would mention that it wasn't, up front.
Think about readers when writing. Authors who only write for themselves are
less likely to sell.
> There are just some circumstances where this is a convenient and sound
> technique to prevent an error like freeing the same block twice.
Yes and Richard Stevens' Advanced Programming for Unix basically says this,
without saying it explicitly; it's definitely implied.
> For example if in an application there are two distinct conditions
> that cause the destruction of the same object, then setting the
> pointer to this object to NULL after freeing the allocated memory
> would signal that the object does not have to be freed once
> the other condition occurs. It is just a cheap way to do bookkeeping.
> An important condition for the validity of this technique
> is that the object is referred to by at most one pointer.
That's a fairly concrete example, however without actually seeing a sample of
the code, it's also difficult to say if the example is entirely accurate.
After all, visual inspection or verification, for the purpose of validation,
is a concept which exists for very good reasons, and some people do
incorrectly use two consecutive if's instead of using an if ... else if ...
construct when the latter is more logically appropriate. I have seen such
code and although it nonetheless worked reliably it was wrong in principle.
Like the concept of leading by example, one I principle I find invaluable, I
also support teaching by example. After all, people can learn quickly with
examples, and pictures sometimes convey a thousand words, or more.
Hopefully, this will help those who can benefit. I am not personally
benefiting, however when someone asks a question, I tend to refrain from the
minimalistic approach when it's appropriate to do so.
If the shoe fits, then why not wear it, I'll say. If it's necessary to
redefine the pointer to null immediately after calling free, then do so;
plain and simple. However, if it's not necessary, then don't clutter
programs with unnecessary code; plain and simple.
Pretty plain and simple, eh.