Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Matsen

How to tell if a pointer is valid

This topic is 5855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Is it possible to determine if a pointer is valid and if it''s not, maybe throw an exception? Here is what I mean: Lets say I have a class... class MyClass { public: void function(); } Now, somewhere, someone does this to a pointer, pointing on an instance of the class above... ptrMyClass = 1; ...and later on, when trying to access function() if (ptrMyClass != NULL) ptrMyClass->function(); .. crash Is there a way to tell that ptrMyClass isn''t a valid pointer? NULL doesn''t help much, and I think I''ve read here somewhere that using NULL as a guarantee for non-valid pointers isn''t the best way. What is the best (or correct) way? Best regards Mats

Share this post


Link to post
Share on other sites
Advertisement
Well, there''s only so much you can do. If you initialize your pointers to NULL, then checking for NULL will work in most cases. Of course, if you''re creating something, then deleting it an not setting the pointer back to NULL, or if someone else is pointing to the same data, there''s nothing you can do.

Share this post


Link to post
Share on other sites
You could do a dynamic_downcast surrounded by a try { } catch (...) block, but that''s really overkill. If somebody does that to a pointer to your class, you''re entitled to crash :-)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If u''re on windows, there are the IsBadRead/WritePtr api

Share this post


Link to post
Share on other sites
if someone does that then they are being mailicious.

what about overloading the assignment operator? slightly related, i remember reading an article where a class that was returned from a function by value would log an error if it wasn''t assigned to another object.

maybe you could prevent someone assigning an integer to an object pointer.

any ideas anyone?

Share this post


Link to post
Share on other sites
quote:
Original post by Matsen
Now, somewhere, someone does this to a pointer, pointing on an instance of the class above...

ptrMyClass = 1;

That''s not actually going to compile. The only means of converting an integral expression to a pointer is via a reinterpret_cast, which would most likely represent malicious (or very stupid) intent.
quote:

Is there a way to tell that ptrMyClass isn''t a valid pointer?

Not in the sense that you are thinking, but you might like to learn a little about smart pointers which may be useful in some of the pointer-related headaches you are bound to encounter.


[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
That''s not actually going to compile. The only means of converting an integral expression to a pointer is via a reinterpret_cast, which would most likely represent malicious (or very stupid) intent.



No, it didn''t (ptrMyClass = (MyClass*)1);

The example might not have been the best, but you understood what I meant. I was just curious to know if it was possible, and the conclusion is that it''s not, at least not without using os specific functions.

Thanks

Regards Mats

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
The only means of converting an integral expression to a pointer is via a reinterpret_cast, which would most likely represent malicious (or very stupid) intent.



Real-mode memory-mapped IO, or driver implementations which require absolute addresses. (OK, I''ll admit it''s a bit outside the scope of these forums )


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
Real-mode memory-mapped IO, or driver implementations which require absolute addresses. (OK, I''ll admit it''s a bit outside the scope of these forums )

Which is why I said "most likely". I did, of course, leave out the "least likely" interpretation, which is that the person genuinely knows what they are doing.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!