Re: free()



Richard Bos wrote:
"santosh" <santosh.k83@xxxxxxxxx> wrote:

Racaille wrote:

On Feb 10, 2:13 pm, "santosh" <santosh....@xxxxxxxxx> wrote:

things yourself. One safe practise is to immediately set a pointer to
NULL after calling free() on it. Passing a null pointer to free() is
harmless.

this is a very bad idea.

instead of having your code crash like hell in the first phases of debugging,
which will allow you to fix the thing quick & properly, you will happily
call free() many times on the same pointer, and build crap around that broken
logic, and then call for a garbage collector :)

I argue that attempting to ensure that pointers are either null or
have legally deferencible values is better.

The problem with that is "attempting". You will, sooner or later, fail
in your attempt. And then, because you believe that all your pointers
are either null or valid, you will not be able to find the bug until
someone else points it out to you. Making sure that you know which of
your pointers are still in use _is_ possible, and does not have this
problem.

Can you cite an example where setting a pointer to NULL could mask a
bug? The way I see it is there are three things you can do with a
pointer, test it, dereference it and free it. Once set to NULL, the
first to will fail and the third is harmless.

--
Ian Collins.
.



Relevant Pages

  • Re: Calling free() on an uninitialised pointer?
    ... >>> If I call freewith a uninitialised pointer, ... >> harmless. ... there are cases where doing so will mask a logic error ... be freed twice, so this is hardly a "mask" - it actually changes the ...
    (comp.lang.c)
  • Re: Calling free() on an uninitialised pointer?
    ... If I call freewith a uninitialised pointer, ... harmless. ... so this is hardly a "mask" - it actually changes the ... freethe memory associated with a pointer more than once? ...
    (comp.lang.c)
  • Re: free()
    ... Passing a null pointer to freeis ... harmless. ... instead of having your code crash like hell in the ...
    (comp.lang.c)
  • Re: IBM Mainframe DSLIST to Dataset w/ Wildcards?
    ... >> What I am looking for is a pointer to a way to improve what I have come up ... 'harmless joshin'' on Mr Dilworth's part... ... Prev by Date: ...
    (comp.lang.cobol)
  • Re: OT: my new PC rocks!!
    ... Microsoft basically "owns" the ACPM thing and has the hardware ... for practically everything;) doing all the work and the "Windows" bit ... is merely the pretty graphics and mouse pointer routines to stare at ... actually seen it up and running to test if this "bug" is even older ...
    (alt.lang.asm)