[wild pointers, what do do?]
As others have pointed out, there is no standard way to examine
a pointer and know it is or is not valid. Thus, you must do the
work yourself in your code. How you do that depends on a lot of
things.
At one level, you just never make a memory location invalid until
you are done with it. So, in broad strokes, it looks like so:
- get the memory
- a lot of stuff happens
- give the memory back
- no more references to the memory
Sometimes it is hard to arrange your code this way. One trick
to make it easier is to restrict the places you use pointers.
For example: You could make every pointer a data member of a
class, and put lots of guardian code in the class. You can then
get into various things like smart pointers, reference counts,
and so on. How sophisticated you make this depends on how
critical it is to be able to demonstrate you don't have any
wild pointers in your code. And how much overhead, in terms of
code and execution time, is acceptable.
Smart pointers and reference counts get discussed here fairly
frequently. There is also some discussion of them in the FAQ.
Search back through google archives to find something applicable
to your particular task.
Another possible choice is to reduce the places you use pointers.
For arrays, for example, you should be looking at the standard
container templates, and avoid using arrays you allocate yourself.
The implementers of the standard library have gone to a lot of
work to make it efficient, flexible, etc. You should consider
using it instead of allocating your own arrays.
Socks