Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Warnning dynamic casting

This topic is 4764 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

I get the following warnning, basically saying my code will have a bug: "warning C4541: 'dynamic_cast' used on polymorphic type 'class BaseClass' with /GR-; unpredictable behavior may result" What is /GR-, and how do I change my settings that this will work good(without a bug)?

Share this post


Link to post
Share on other sites
Advertisement
It will slow the casting down ever so slightly, but probably not enough to be worry about. It might also make your objects bigger, but you may have already paid that price by having virtual functions, I'm not sure. It really doesn't matter since it looks like you need RTTI, and don't have much of a choice.

Share this post


Link to post
Share on other sites
smart_idiot: I was under the impression that enabling RTTI had no effect (preformance or otherwise) on casting except actually enabling dynamic_casts sideways/upwards (which not working without RTTI can't be directly compared)?

Sane compilers should deduce type from the vtable pointer (and since dynamic_cast only works with polymorphic types - e.g. those types allready having vtables - this will mean no extra per-object overhead, only per-type).

The only reasons I'm aware of to disable RTTI:
*) Executable size (RTTI increases EXE size some (don't ask me how much, check for yourself as it will vary by project))
*) Target platform has no support for RTTI (e.g. certain embedded targets, ancient/nonstandard compilers, etc).

/GR may slow down your program slightly due to increased cache misses (e.g. the RTTI info happens to cause two bits of data to reside on different pages), but not a significant amount unless you've got a pretty whacky compiler.

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
smart_idiot: I was under the impression that enabling RTTI had no effect (preformance or otherwise) on casting except actually enabling dynamic_casts sideways/upwards (which not working without RTTI can't be directly compared)?

Sane compilers should deduce type from the vtable pointer (and since dynamic_cast only works with polymorphic types - e.g. those types allready having vtables - this will mean no extra per-object overhead, only per-type).


The vtables pointed to by polymorphic types (MS compiler) are actually an offset into the types RTTI data structure. The first 4 bytes of the data structure (vtable - 4) point to the RTTI object descriptor. This contains pointers to other information such as the class hierarchy, a pointer to the base classes rtti descriptor, and a pointer to a const instance the std::type_info object.

When a dynamic cast is done the performance can be primarily gauged by the distance between the relationship of object being cast and the type it is being cast to. One of the more inefficient calls made during the cast is in FindSITargetTypeInstance() which is internal to the runtime. Depending on the distance one or more calls to strcmp will be made on the contents returned by type_info.raw_name() not to mention the amount of searching done through the heirarchy of both class types involved in the cast.

Share this post


Link to post
Share on other sites
Quote:
Original post by Helter Skelter
Quote:
Original post by MaulingMonkey
smart_idiot: I was under the impression that enabling RTTI had no effect (preformance or otherwise) on casting except actually enabling dynamic_casts sideways/upwards (which not working without RTTI can't be directly compared)?

Sane compilers should deduce type from the vtable pointer (and since dynamic_cast only works with polymorphic types - e.g. those types allready having vtables - this will mean no extra per-object overhead, only per-type).


The vtables pointed to by polymorphic types (MS compiler) are actually an offset into the types RTTI data structure. The first 4 bytes of the data structure (vtable - 4) point to the RTTI object descriptor.


That was what I meant by "from the vtable pointer" :-).

Quote:
When a dynamic cast is done the performance can be primarily gauged by the distance between the relationship of object being cast and the type it is being cast to. One of the more inefficient calls made during the cast is in FindSITargetTypeInstance() which is internal to the runtime. Depending on the distance one or more calls to strcmp will be made on the contents returned by type_info.raw_name() not to mention the amount of searching done through the heirarchy of both class types involved in the cast.


But this isn't anything that should affect the speed of casts which by their nature work without RTTI, correct? And those requiring RTTI won't work any faster if RTTI is disabled, they just plain won't work :-).

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
smart_idiot: I was under the impression that enabling RTTI had no effect (preformance or otherwise) on casting except actually enabling dynamic_casts sideways/upwards (which not working without RTTI can't be directly compared)?
Well I thought that a dynamic cast mostly only differs from a static cast in that it checks that the object is of the requested type. If you know for certain it is beforehand, then other zero-overhead casts can be used.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Quote:
Original post by MaulingMonkey
smart_idiot: I was under the impression that enabling RTTI had no effect (preformance or otherwise) on casting except actually enabling dynamic_casts sideways/upwards (which not working without RTTI can't be directly compared)?
Well I thought that a dynamic cast mostly only differs from a static cast in that it checks that the object is of the requested type. If you know for certain it is beforehand, then other zero-overhead casts can be used.


Oh, gotchya, you're comparing:

parent * something_that_i_know_to_be_a_derived = ...;
derived * d1 = static_cast< parent * >( something_that_i_know_to_be_a_derived );
to:
derived * d2 = dynamic_cast< parent * >( something_that_i_know_to_be_a_derived );

Indeed, the d2 version will be slower since the compiler isn't making the same assumption (that is, that something_that_i_know_to_be_a_derived is in fact a derived). But this requires actually switching the cast used (e.g. using dynamic_cast), rather than just enabling RTTI (/GR).

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!