Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2006

Tip: Looking for answers? Try searching our database.

Checking Bad Pointers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike C# - 28 Jun 2006 21:31 GMT
Is there a better way than IsBadReadPtr() to check for a bad pointer?

Thanx
William DePalo [MVP VC++] - 29 Jun 2006 00:33 GMT
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.