Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Type casting performance (void* -> struct*)

Credits go to Audric, Oscar Giner, and SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
Type casting performance (void* -> struct*)
AMCerasoli
Member #11,955
May 2010
avatar

Guys!

I'm not experienced in this stuff, and I have to admit that I'm probably getting in a very obscure path with my current game... The thing is now I'm using vectors of void pointers because all this polymorphism and inheritance stuff is really annoying.

So I'm trying new stuff to get experience and such. The thing is I'll need to cast 60 times per second void pointers to an struct/class pointer.

TERRAIN_COMP *tr = (TERRAIN_COMP*) it2->second;

I was wondering if I'm killing my game with this. Because this gives me a lot of freedom and I would sacrifice some performance if it's not too much.

But I don't have the experience to really know if this is a good idea. And I don't want to base my game in something that is not going to work.

So, if there is someone who knows if this actually affects performance please tell me. Thanks.

SiegeLord
Member #7,827
October 2006
avatar

Unless you overload that operator (if that's even possible in C++) then it has no or negligible performance penalty. The only way I can think of it mattering is if the compiler can do some weird optimization that requires the knowledge of the type... but that sort of thing that's not available in your design anyway, cast or no cast.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Audric
Member #907
January 2001

This kind of cast doesn't cause any additional instruction to be performed at run time. A pointer is not a C++ object, it's just a 4-byte (or 8-byte) piece of memory that contains a memory address.
However, you're choosing the path of no type safety. If you makes any mistake along the way, you'll get no compilation error/warning, only wacky behaviors and crashes.

AMCerasoli
Member #11,955
May 2010
avatar

Oh I see, nice. Then let's type cast everything! ;D No really. I think that everything is under control... :P

Oscar Giner
Member #2,207
April 2002
avatar

Pointer casts are just a compile time thing (well, except C++ dynamic_cast, where runtime checks are performed), so there's no runtime overhead at all (of course in your code there's an extra assignment since you're storing the casted pointer in a new variable, but the optimizer should get rid of that). Pointers are just addresses to memory locations and all pointers (well, again an exception in C++: pointers to member variablesand methods) are actually the same. Pointers have a type just for convenience, as a type checking mechanism.

Or course you should avoid pointer casting, specially in C++ where there are better mechanisms to use. You later may have lots of headaches trying to debug your code.

AMCerasoli
Member #11,955
May 2010
avatar

Yhea, I know what pointers are. I just was wondering if when typecasting the program had to do something in real time; like "preparing" the object for this new pointer or whatever... But since it's a compile time process, couldn't be better.

I still remember when I was scared about pointers... Now I can't live without them...

Or course you should avoid pointer casting, specially in C++ where there are better mechanisms to use.

Ok this is an idea I have and I don't want to start a battle here. But C++ is not good for making videogames... TAN! TAN! TAN!............ . . .

Karadoc ~~
Member #2,749
September 2002
avatar

On the topic of C++ vs C...

(short version: I don't dislike C++ anymore. I think C++'s strength starts to show in large projects.)

In the not-so-distant past, I had great confidence in my ability to use pointers in weird and wonderful ways without making mistakes; and I preferred programming in C rather than C++, because I felt that the additional syntactic sugar of C++ basically just led to 1,000,001 bizarre looking ways of doing the same stuff C can do anyway; and the compiler error messages in C++ can be extremely obscure.

But now the more I learn about C++ the more I prefer it to C. I think the turning point was when I started browsing this faq about C++11 (which I started reading when it was C++0x, before it became the official standard). Not only do the new features help to clean up some of the ugliness in C++ syntax, but also some of the reasoning in that faq helped me to understand why some of the C++ stuff exists in the first place.

I think the big thing that I didn't really understand back when I was a diehard C fan was just how important coding style is for the scaling of large projects. A lot of the syntactic bloat that I hated about C++ turns out to be pretty useful for minimizing the opportunity for really nasty programming mistakes to arise unnoticed.

-----------

Go to: