H
Henrik Goldman
Hi,
I have a piece of code which looks like the following:
void f2(int i)
{
}
void f1(HANDLE h, const void *pSetting1, const void *pSetting2)
{
switch(...)
{
case ABC:
f2((int) pSetting1);
}
}
Assuming f1 takes a void pointer (actually two of them but it doesn't
matter) I need to call f2 where I know that the variable really holds an
integer.
Due to compatibility with C the function f1 needs to be with C declaration
and for this reason I cannot use function overloading with different
parameters depending on the input type. Therefore it's assumed that f1() can
take input of various types such as integers or pointers of specific types.
In the example above I want to cast the void pointer to an integer but this
fails on 64 bit platforms due to the fact that it goes from 64 bit to 32.
On platforms with gcc I've previously used constructs like (int) (unsigned
long) since unsigned long is 64 bit wide on those platforms. However this
could cause problems with Win64 since Microsoft uses another 64 bit model
then commonly used under unix.
How would I cast this in platform independent way to ensure that the result
would be as expected on both 32 and 64 bit platforms?
Thanks.
-- Henrik
I have a piece of code which looks like the following:
void f2(int i)
{
}
void f1(HANDLE h, const void *pSetting1, const void *pSetting2)
{
switch(...)
{
case ABC:
f2((int) pSetting1);
}
}
Assuming f1 takes a void pointer (actually two of them but it doesn't
matter) I need to call f2 where I know that the variable really holds an
integer.
Due to compatibility with C the function f1 needs to be with C declaration
and for this reason I cannot use function overloading with different
parameters depending on the input type. Therefore it's assumed that f1() can
take input of various types such as integers or pointers of specific types.
In the example above I want to cast the void pointer to an integer but this
fails on 64 bit platforms due to the fact that it goes from 64 bit to 32.
On platforms with gcc I've previously used constructs like (int) (unsigned
long) since unsigned long is 64 bit wide on those platforms. However this
could cause problems with Win64 since Microsoft uses another 64 bit model
then commonly used under unix.
How would I cast this in platform independent way to ensure that the result
would be as expected on both 32 and 64 bit platforms?
Thanks.
-- Henrik