Checking Bad Pointers

  • Thread starter Thread starter Mike C#
  • Start date Start date
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
 
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.
 
Mike C# said:
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
 
William DePalo said:
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.
 
Back
Top