> 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
Mike C# - 29 Jun 2006 01:07 GMT
I'm calling EnumPrinters() Win32 API function. Some of the pointers in its
PRINTER_INFO_n structure might be invalid, depending on the properties of
the printer enumerated.
>> Is there a better way than IsBadReadPtr() to check for a bad pointer?
>
[quoted text clipped - 22 lines]
> Regards,
> Will
William DePalo [MVP VC++] - 29 Jun 2006 02:53 GMT
> I'm calling EnumPrinters() Win32 API function. Some of the pointers in
> its PRINTER_INFO_n structure might be invalid, depending on the properties
> of the printer enumerated.
First, I have to tell you that I don't do a lot of GDI so I am not familiar
with the function. I can tell you that you should not have to validate with
the IsBad... functions the pointers that you get back from successful calls
to functions in the Win32 API.
I'd expect that the pointers it returns are either good or have a value of 0
(i.e. NULL). It might be the case (I doubt it) that some other member of the
structure would tell you to ignore a particular pointer but that's a
stretch.
Do you have a case where a non-zero pointer returned by the function is bad?
Regards,
Will
Mike C# - 29 Jun 2006 04:57 GMT
> Do you have a case where a non-zero pointer returned by the function is
> bad?
I'll have to double-check, but I do believe it returned a different invalid
pointer address for at least one member of the PRINTER_INFO_n structure. I
believe it was 0xccccccccc... I'll confirm that tomorrow.