Creating & deleting pointers

Started by
29 comments, last by Brother Bob 11 years, 8 months ago

[quote name='dmatter' timestamp='1343425121' post='4963771']
Not sure I buy that argument really, how would you manage to use the pointers after the destructor? It's only really relevant if you call delete on a pointer where subsequent code still has access to that pointer - such a scenario, whilst easy to concoct, should be almost always avoided.

I agree, of course. Maybe I wasn't clear in my description, I don't advocate it as a "design pattern". What I meant is that it helps you find bugs, while not being something you should rely on.
[/quote]
It also hides bugs.

Deleting a null pointer is legal and guaranteed by the language to be a no-op. So you if you're double-deleting a pointer in some code path, you have hidden that bug and you won't discover it. At least pick a non-null pointer if you want to clear a pointer to some easy-to-spot value so that it at least is likely to crash or do something to get your attention.

This topic is closed to new replies.

Advertisement