0

Possible Duplicate:
Is it good practice to NULL a pointer after deleting it?

My professor told us that it's good practice to set a pointer to 0 after we've deleted the allocated space it was pointing to and I've been trying to make a habit out of doing this. But when I do this, my compiler sends a warning my way:

Warning W8004 LinkedList.h 102: 'nPtr' is assigned a value that is never used in function LinkedList::remove(int)

I know warnings aren't the end of the world and my program will still compile, but my OCD just will not let it go. So I ask you more knowledgeable programmers:

Is it common to set a pointer to 0 after deleting it's node and is that really a good practice? Does it matter if I continue to let my programs compile with warnings such as this? Thanks to all answers!

Community
  • 1
  • 1

4 Answers4

9

It is common. It is not, IMHO, good practice.

Good practice is to arrange your deletes in such a way that you already know the pointer can't be used after the deletion. The best way to do that is to use RAII, i.e. do the work in a destructor. Once the destructor reaches the end, the object no longer exists, therefore the pointer (being a data member) no longer exists, therefore it is not dangling.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • sorry about my ignorance, but what's RAII? –  Dec 01 '10 at 03:30
  • 3
    @blakejc70: Resource Acquisition Is Initialization. Basically, the idea is that you encapsulate resources in objects so they are destroyed when the object's destructor is called. In this way, you can declare the object in local scope and not need to worry about cleaning up resources when you're done with them. (They're cleaned up at the end of scope.) This is the basis for some kinds of smart pointers like `auto_ptr` and `scoped_ptr`. – greyfade Dec 01 '10 at 03:49
  • Oh cool, thanks. I had heard something about that before, I think, but I have never heard it called anything like that. –  Dec 01 '10 at 04:04
0

setting pointer variables to null when the thing they used to point to has been deallocated is standard but so you don't need to let that warning bug you in this case but it is useful for finding varibles that are assigned to but never used

Dan D.
  • 73,243
  • 15
  • 104
  • 123
0

Just guessing, the warning is about a local variable? because if it's not, i don't know how the compiler could know the assigned value (NULL) wasn't used somewhere else.

It's (IMHO) a significant part of the question, because the point of NULLing pointers is so no other part of the code can (mistakenly) use a no longer defined address; if the pointer isn't accessible from anywhere else (ie, a local variable), then there's no reason to protect against it.

It could be argued that even within a function there's some value in protecting against further use; but good style dictates very short functions, where it's obvious at first sight that the use of the variable ends right there.

Javier
  • 60,510
  • 8
  • 78
  • 126
  • oh good point, I delete the allocated space and the pointer falls out of scope anyway, so in a situation like that the null assignment doesn't matter anyway, thanks! –  Dec 01 '10 at 03:32
0

It depends.

While you should, as Karl said, arrange your deletions so you don't risk using already-deleted pointers, it can be useful if an object may not have the same lifetime as its parent or a variety of other situations.

Since a pointer to NULL will evaluate false in an if, you can delete objects and set the pointer to NULL at one spot, then test if the object still exists at another point.

The warning you're getting is harmless but may indicate an inefficient bit of code. You may want to look at it to see if you need to have that pointer or need to set it at all.

As far as it being good practice, IMO it is for clarity. When you delete an object, set any pointers to NULL so you know they haven't been leaked and are deleted. It's not necessary and using auto_ptrs or shared pointers will avoid the whole issue.

ssube
  • 47,010
  • 7
  • 103
  • 140