Jump to content
  • Advertisement
Sign in to follow this  
Donna

TypeCast

This topic is 2143 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 all,

Can anyone explain me how and why I have to typecast a variable "StartTime" in the following function:


__int64 StartTime;

BOOL WINAPI QueryPerformanceFrequency( __out LARGE_INTEGER *lpFrequency);

Thanks a lot

Donna

Share this post


Link to post
Share on other sites
Advertisement
LARGE_INTEGER is a union that contains a 64-bit integer, it's not actually a 64-bit integer type itself.

Share this post


Link to post
Share on other sites
Don't typecast here, use the correct type for the function. You should never use a typecast to get this kind of thing to work unless you are 300% sure of what you are doing.

Consider what SiCrane says. If you pass a pointer to a value with only 8 bytes to a function that expects to be able to write more than 8 bytes, then other values in memory nearby can and will be corrupted.

Share this post


Link to post
Share on other sites

Don't typecast here, use the correct type for the function. You should never use a typecast to get this kind of thing to work unless you are 300% sure of what you are doing.

Consider what SiCrane says. If you pass a pointer to a value with only 8 bytes to a function that expects to be able to write more than 8 bytes, then other values in memory nearby can and will be corrupted.


Hi Rip off,

Well I was thinking to use the function like this....

QueryPerformanceFrequency( (LARGE_INTEGER *) &StartTime);

So In order to to pass met __in64 variable I should typecast it to a LARGE_INTEGER.....
I'm not passing a pointer with a 8 bits value to this function

If I'm not correct...let me know

Thanks a lot

Share this post


Link to post
Share on other sites
First off, I was incorrect in my statement. LARGE_INTEGER appears to be 8 bytes long. However, I still would advise against using a type cast for this.

A simple solution would be to change the type of StartTime to LARGE_INTEGER. Alternatively, if you don't want to deal with the various members of this union, call QueryPerformanceFrequency with a temporary LARGE_INTEGER and extract the QuadWord value into your StartTime value:

LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
StartTime = frequency.QuadPart;

The danger with typecasts is that the compiler cannot help you. Lets say you change the type of StartTime to a smaller or larger number of bytes. Or even change it to unsigned. The change could be intentional (e.g. changing the type when porting your software) or accidental (i.e. copy/paste, find/replace mistakes).

Typecasting like that should be a last resort and done for very good reasons. Do you have a good reason?

Also, that you are trying to write a frequency to a variable named time is also suspicious.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!