Re: free()



"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.

Richard
.



Relevant Pages

  • Re: programming concepts > specific languages
    ... > pointers if you are just doing java all along. ... Java has consistent, but non-intuitive, rules involving passing by ... value/passing by reference. ... A lot of beginning Java programmers don't ...
    (comp.programming)
  • Re: is forth a functional language?
    ... to pass it upwards as a function result (if that is possible in the ... type system that simply precludes you from passing a local function ... instantiating generics or by passing around pointers. ... lexical scoping, but I'm certainly splitting hairs here. ...
    (comp.lang.functional)
  • Re: no pointer in Java => my problem
    ... >> the value you are passing is an adress and you can't change the ... When passing a reference, you don't get ... > code anyway, where there are no pointers or references, just bit ... can design applications using objects and still implement them in C. ...
    (comp.lang.java.programmer)
  • Re: Trouble with Java concepts
    ... >>is functionally identical to passing pointers in C. ... I should have said that passing object in java is functionally equivalent ... For a newcomer from C/C++, I think it helps to be able to compare what ...
    (comp.lang.java.programmer)
  • Re: typedefs and structs
    ... > help avoid accidentally passing null pointers. ... > function declarations as: ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)