Sign in to follow this  
Donna

TypeCast

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
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
[quote name='rip-off' timestamp='1344422252' post='4967332']
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.
[/quote]

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:
[code]
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
StartTime = frequency.QuadPart;
[/code]
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 [u][i]frequency[/i][/u] to a variable named [u][i]time[/i][/u] is also suspicious.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this