🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Casting or type conversion question

Started by
5 comments, last by fleabay 4 years, 4 months ago

Hello,

can someone explain me if there are differences in execution speed between following 3:

int a=3;
size_t b=(size_t)a;
size_t b=size_t(a);
size_t b=static_cast<size_t>(a);

I am pretty much sure that there is no difference between #1 and #3. But for #2, doesn't that add an overhead because of the constructor?

And what about following example?

unsigned char* a=aRandomBuffer;
float b=((float*)a)[0];
float b=(reinterpret_cast<float*>(a))[0];

Thanks for any insight!

Advertisement

There should not be any difference in execution speed in either example. The compiler will almost certainly generate the exact same code for all three cases in the first example, and for both cases in the second example.

Thanks for the clarification!!

It easy to try out these types of things and examine the assembly yourself: https://godbolt.org/z/nGiDQu

@codingjoe I heard casting is less reliable, i also read some of this if you wish to find out more about the reliability of casting. There is a lot of great explaining of how casts operate here.

please like if useful.

https://stackoverflow.com/questions/4167304/why-should-casting-be-avoided

DoomOperator said:
please like if useful.

I've heard that it's not useful.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

This topic is closed to new replies.

Advertisement