Mike C# said:
Is there a better way than IsBadReadPtr() to check for a bad pointer?
Well, that begs the question as to what you know about the pointer's
provenance.
So where did you get it from and do you trust the source?
Of course you could cook up your own pointer validation function but that
would probably be as expensive at runtime as IsBadReadPtr(). Bad pointers
are signs of programming errors and the best bet is to write bug free
programs.
Of course, none of us can do that so there is a school of thought that says
that you should never use a "naked" pointer in a C++ program. Rather, you
should use a smart pointer class. There the constructor either creates a
good pointer, throws an exception or initializes it to something like zero
that makes it easy to spot the fact that the pointer is bogus. With a smart
pointer class, you are less likely to introduce the bugs that come from
failing to assign a pointer properly.
Just btw, I have yet to see a non-trivial C++ application without a single
naked pointer. Of course, I haven't seen them all.
Can you talk at all about the context in which you need to validate the
pointer? Perhaps if you do someone will be able to suggest something.
Regards,
Will