Re: Set a pointer to null when deleting?
andrew queisser wrote:
[color=blue]
> I'm a little surprised at the responses you've been getting from some very
> experienced programmers.[/color]
Some of us know the textbook answer (set the friggin' pointer to NULL),
while some of us can't remember the last time we actually did that. There's
more reasons than smart pointers.
Another alternative is to set the pointer equal to the address of a static
NullObject. Consider this code:
p = getPointerFromW hatever();
if (p)
p->method(42);
Using NullObjects, that becomes this:
p = getPointerFromW hatever();
assert(p);
p->method(42);
The code is one tick simpler; it has one less 'if' statement.
Replacing conditional statements with the interactions of virtual methods is
what OO is all about.
[color=blue]
> I think the correct answer to your question should be: if you intend to
> check the value of the pointer later on you should set it to NULL[/color]
Even if you don't intend to, if that pointer has remaining scope, set it to
NULL to make sure (on common implementations ) you get a clean crash and not
memory corruption if you then accidentally try to dereference the pointer.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
andrew queisser wrote:
[color=blue]
> I'm a little surprised at the responses you've been getting from some very
> experienced programmers.[/color]
Some of us know the textbook answer (set the friggin' pointer to NULL),
while some of us can't remember the last time we actually did that. There's
more reasons than smart pointers.
Another alternative is to set the pointer equal to the address of a static
NullObject. Consider this code:
p = getPointerFromW hatever();
if (p)
p->method(42);
Using NullObjects, that becomes this:
p = getPointerFromW hatever();
assert(p);
p->method(42);
The code is one tick simpler; it has one less 'if' statement.
Replacing conditional statements with the interactions of virtual methods is
what OO is all about.
[color=blue]
> I think the correct answer to your question should be: if you intend to
> check the value of the pointer later on you should set it to NULL[/color]
Even if you don't intend to, if that pointer has remaining scope, set it to
NULL to make sure (on common implementations ) you get a clean crash and not
memory corruption if you then accidentally try to dereference the pointer.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Comment