Jump to content
  • Advertisement
Sign in to follow this  
lXciD

Unity Casting void pointers

This topic is 3302 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 all, I am doing a lot of reading on casting on pointers and are confuse. So I thought I come here to get some views from the community. In my post here, I will not touch on reinterpreted and const cast, just the C cast, static cast and dynamic cast. From my understanding, casting on pointers will make compiler change its view on the type the data is holding. So for example if I use C cast to cast object of class A to an unrelated class B. It will succeed and think the object is now class B even though its illegal from our perspective. (I understand this is reinterpreted cast in C++ but lets just focus on C cast) As for related classes, Casting between a base class to a derived class is more of adding and subtracting pointers. First of all, I read that casting only return 0 if you are using dynamic cast because of the RTTI. Other than that the application have no idea whether a casting succeed or fails on run time. This make me wonder, if you are not using RTTI and dynamic casting, why bother checking for null pointer after a cast? for example...
DerivedClass * d = ( DerivedClass * ) b;
// DerivedClass * d = static_cast<DerivedClass *>( b );
if ( d != NULL )
// if ( d )
{
}
So I conclude that its useless to check for null pointer to determine if a casting succeed or fail. But its a good practice so that if you ever enable RTTI and dynamic cast, your code will become safer. Secondly about void pointer. Casting void pointer essentially falls under the category casting between unrelated pointers. For the following operation:
DerivedClass * derviedObject = new DerivedClass(); // Derived from BaseClass

void * userData = ( void * ) derivedObject;
// void * userData = static_cast<void *>( derivedObject );

BaseClass * baseObject = ( BaseClass * ) userData; // baseObject pointing wrongly
If I were to cast from a derivedObject to a void pointer and then cast the void pointer to a baseObject (a BaseClass pointer), access violation will occurs on baseObject due to there's no adding and subtracting operation occurring on the pointer in question happening during the casting operation (this operation is required for casting between related pointer) as all the operation is treated as unrelated pointers. In order for the following codes to work, I need to store the base pointer in the void pointer and cast it back to base pointer.
DerivedClass * derviedObject = new DerivedClass(); // Derived from BaseClass

void * userData = ( void * ) ( BaseClass *) derivedObject;
// void * userData = static_cast<void *>( static_cast<BaseClass *>( derivedObject ) );

BaseClass * baseObject = ( BaseClass * ) userData; // work
// BaseClass * baseObject = static_cast<BaseClass *>( userData ); // work

DerivedClass * derivedObject2 = ( DerivedClass * ) baseObject; // work
// DerivedClass * derivedObject2 = static_cast<DerivedClass *>( baseObject ); // work

DerivedClass * derivedObject3 = ( DerivedClass * ) userData; // don't work
// DerivedClass * derivedObject3 = static_cast<DerivedClass *>( userData ); // don't work
Of course, this mean that direct casting of userData to DerivedClass pointer will fail too. Would like to ask if I got the right interpretation? :) Please correct me if I'm wrong. Sorry for my poor english. Reference: http://www.codeproject.com/KB/cpp/static_cast.aspx Edit: One more question. If RTTI is enabled. Will casting a void pointer storing a base pointer to its derived pointer and vice versa work?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by lXciD
if you are not using RTTI and dynamic casting

dynamic_cast is impossible without RTTI.

Quote:
Original post by lXciD
Will casting a void pointer storing a base pointer to its derived pointer and vice versa work?

You mean something like Base* -> void* -> Derived* ?

It will certainly blow up with multiple inheritance. With single inheritance, you may get the desired results on your platform, but I think the behavior is technically undefined. I may be wrong, but I seem to remember that the only safe conversion is

myclass* -> void* -> myclass*

i.e. you have to cast back to the exact same type as before.

Share this post


Link to post
Share on other sites
Quote:
So I conclude that its useless to check for null pointer to determine if a casting succeed or fail.

I conclude that you need to read the standard with respects to dynamic cast, DevFred is correct it requires RTTI and a polymorphic type. There is another use for dynamic cast when you cast to a void pointer it returns a pointer to the most derived class.

Quote:
If I were to cast from a derivedObject to a void pointer and then cast the void pointer to a baseObject (a BaseClass pointer), access violation will occurs on baseObject

Casting from A to void to B is undefined, unless B is of type char( or a variant) or you know enough about your compiler because it could be implementation specific. Also casting to a void pointer is implicit and there is not need for static_cast in your example.

Quote:
... access violation will occurs on baseObject due to there's no adding and subtracting operation occurring on the pointer in question happening during the casting operation (this operation is required for casting between related pointer) ...

You are talking about implementation specific behaviour here.

Quote:
One more question. If RTTI is enabled. Will casting a void pointer storing a base pointer to its derived pointer and vice versa work?

Implementation defined, a void pointer has no type information.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!