Skip to main content
GameDev.net gamedev.net
🔒 Locked

Casting or type conversion question

Started by codingJoe Feb 20, 2020 at 3:22 PM 5 replies 2.2k views
Original Post
codingJoe
codingJoe

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!

a light breeze
a light breeze

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.

codingJoe
codingJoe

Thanks for the clarification!!

fleabay
fleabay

DoomOperator said:
please like if useful.

I've heard that it's not useful.

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.