This is the mail archive of the glibc-linux@ricardo.ecn.wfu.edu mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: malloc problem/question in 2.1.3


On Sat, Jun 03, 2000 at 07:13:51PM -0400, Mike Corbeil wrote:
> Ronald de Man wrote:
> 
> > On Fri, Jun 02, 2000 at 07:45:34PM -0400, Mike Corbeil wrote:
> > > Wolfgang Sourdeau wrote:
> > >
> > > >     Mike> As a short aside, I think that this could be checked for in
> > > >     Mike> Perl using "defined" (e.g., if defined $var ...), however C
> > > >     Mike> is a compiled language and I don't think there's any such
> > > >     Mike> functionality, not afaik anyway.
> > > >
> > > > One could implement this by writing a wrapper for malloc and free
> > > > which would do the accounting. Another way to do that is to put a
> > > > pointer to NULL directly after it is freed and it is even easier. Be
> > > > careful though to make this coherent with the whole code.
> > >
> > > If a job must be done in C and such functionality is desired, then a
> > > wrapper is probably an acceptable solution.  I've used and created
> > > wrappers before, however thought the programmer wanted built-in
> > > functionality, and wrappers are an elementary concept, but not
> > > built-ins.  (Wrapping is actually, conceptually, inherent in all
> > > programs using functions or procedures, and a program itself is a
> > > wrapper, albeit neither is the kind of wrapper Wolfgang referred to.)  A
> > > wrapper is certainly an easy approach.
> > >
> > > However, I'm not sure what is meant by "put a pointer to NULL" after it
> > > is freed.
> >
> > char *ptr = (char *)malloc(100);
> >
> > /* use the 100 allocated bytes */
> >
> > free(ptr);
> > ptr = NULL;    /* put ptr to NULL */
> 
> I assumed this was what you meant, but this is unnecessary, unless ptr is
> going to be used again.  One simple case to show how this is, simply is that
> free is often called at the end of functions.  However, even when called
> before the end of a function, there's no logical reason to do this, because
> the pointer was just freed.

The point is that one can now check whether the pointer is still valid
by testing it against the NULL value. This is a way to improve robustness
of the program. (On the other hand it might hide bugs.)

This discussion was about the perceived lack of robustness in free().
For example, programs tend to crash when free()ing memory twice.
Wolfgang pointed out that one common way to prevent this type of
crashes, is to set a pointer to NULL when it is no longer needed.

My example could have been clearer:

if (ptr != NULL) {	/* is this a valid reference? */
  free(ptr);		/* then we can free the memory */
  ptr = NULL;
}

If the memory block had already been freed (so by convention, ptr had
been set to NULL), then this check makes sure it is not freed for a
second time.

> 
> > >
> > > Do you mean to redefine the pointer as the null string, as it would be
> > > done in the initialization of a pointer, and if yes, then why would one
> > > want to do this immediately after calling free, when free is supposed to
> > > free the memory previously reserved for the pointer, after which such a
> > > pointer typically isn't used, at least not until malloc is to be invoked
> > > again for the pointer?

[I am sorry to have disturbed you, but reading the above did give me
the impression that you were mixing up pointers with the memory
blocks pointed at by pointers.]

Note that on the internet it is common practice to refer
people to more appropriate forums. This list is intended for
discussion of the use of the GNU C library.

Thanks,

Ronald


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]