Jump to content
  • Advertisement

Archived

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

Zoomby

dynamic_cast or ordinary cast?

This topic is 5621 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, What is the difference between the two casts below? The only thing I know is, that dynamic_cast can return NULL. But how expensive is it to use dynamic_cast and the other cast operators? Mammal* m=new Cat(); Cat* cat=(Cat*)m; Cat* cat=dynamic_cast(m); bye chris

Share this post


Link to post
Share on other sites
Advertisement
I know a normal cast is free... I'm not to sure about a dynamic_cast.

In binary... all pointers have no type, they are simply memory locations. Doing a normal cast just tells the compiler that you know you are going from one type to another (type checking). It doesn't actually change anything, and still simply sets the pointer cat to the address stored in m. There is nothing else going on behind the scenes.


--- Edit ---
Made a simple typing correction.

[edited by - Ready4Dis on May 6, 2003 10:02:01 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
I know a normal cast is free...

No it is not! A so-called `normal'' cast, by which I assume you mean a C-style cast can mean one of several things. C++ breaks the different forms of cast down into explicit constructs for static_cast, dynamic_cast, const_cast and reinterpret_cast. None of the casts are free by any decent definition of the word. There is always some associated cost, whether it be at compile-time or run-time, or in terms of const-correctness or platform neutrality. There is always a price to pay.
quote:

I''m not to sure about a dynamic_cast.

A dynamic_cast is sometimes a symptom of a poor design. If that is the case, then the cost of dynamic_cast should be the least of your worries. If you genuinely need one, then you pay the price for a quick RTTI check. But then, if you genuinely need one, then there''s no escaping the cost. It''s only unnecessary overhead if you don''t actually need it.

Share this post


Link to post
Share on other sites
He was asking how expensive the casts were, not how long they take to compile. So my statement about it being "free" still stands true. You HAVE to use a cast, so arguing that it takes time to compile is a mute point, because any cast will have to be processed at compile time. He was asking which was more expensive (aka, which takes more clock cycles), and a "normal" cast takes none.

Share this post


Link to post
Share on other sites
I just checked the disassembly of both casts. The "C-Sytle" cast is just copying the adresse, while the dynamic_cast is calling a function which is very large (in assembler).
Now what is this function doing and how fast is it? If there is such a overhead, does anyone use dynamic_cast in time-critical software like games?

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
He was asking how expensive the casts were, not how long they take to compile.

He didn''t say in which sense he meant it. It is perfectly valid for `expensive'' to mean `it makes my program harder to understand'' or `it makes my program take longer to compile'' or even `it costs more money to write''. Some people will happily pay for a miniscule amount of CPU if it helps provide assurances about application stability.
quote:

You HAVE to use a cast, so arguing that it takes time to compile is a mute point, because any cast will have to be processed at compile time.

Nonsense. Some casts can only take place at run-time. In the OP''s case, it''s not exactly clear what sort of cast he wants as he hasn''t provided enough information. It is a downcast, but I don''t know if it will have to be a static or dynamic downcast. If it has to be a dynamic_cast, then the associated cost cannot be escaped.

Share this post


Link to post
Share on other sites
quote:
Original post by Zoomby
If there is such a overhead, does anyone use dynamic_cast in time-critical software like games?
No. In time critical software they don't even call a single function because the overhead is so big. They inline everything by hand just to make sure it gets inlined. And then they code most of the GUI in ASM.



[edited by - civguy on May 6, 2003 10:37:04 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Zoomby
Now what is this function doing

It is checking that the dynamic type of the pointer is either of the type specified in the cast, or an accessible base of the type specified. If it is one of those, then it is yielding a converted pointer, if not it will yield NULL.
quote:

If there is such a overhead, does anyone use dynamic_cast in time-critical software like games?

People tend to try and avoid dynamic_cast in general, if it can be helped. Counter-examples include things like the Visitor idiom, which is an approximation of dual dynamic despatch.

Share this post


Link to post
Share on other sites
Zoomby: What do you use as your main c++ reference? If you''re using microsoft it comes with plenty of material. I''d recommend getting a good book as well though.

Do you know what dynamic_cast does?

It will work out at runtime whether the pointed to object is related to the type you''re trying to cast it to i.e. if it is derived from the type you are casting it to.

If it isn''t it will return 0.
If it is it will return a pointer to the correct type.

Your program must then decide what to do. You can''t use the zero pointer or your program will exhibit undefined behaviour.

If your program requires it there is quite likely something wrong with your design. There are cases where it is arguably the best soultion to a problem but it is unlikely that you are in that territory.

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.

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!