Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Portable typecast

Reply
Thread Tools

Portable typecast

 
 
Henrik Goldman
Guest
Posts: n/a
 
      11-28-2006
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


 
Reply With Quote
 
 
 
 
amparikh@gmail.com
Guest
Posts: n/a
 
      11-28-2006

Henrik Goldman wrote:
> 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


You probably need to post this question on the Microsoft win32
newsgroup. You might find answers there. or else just search for
ULONG_PTR in there. I am sure you would get the answer.

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      11-28-2006
Henrik Goldman napsal:
> 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?


Hi. You can pass in void pointer pointer to int and cast it to correct
pointer type in function.

Something like:

void fn(void* pSettings)
{
int* pInt = (int*)pSettings;
// Use pInt here
}

 
Reply With Quote
 
Henrik Goldman
Guest
Posts: n/a
 
      11-28-2006
> You probably need to post this question on the Microsoft win32
> newsgroup. You might find answers there. or else just search for
> ULONG_PTR in there. I am sure you would get the answer.
>


But it's not a Microsoft specific question. Currently I made things working
on Windows by using: (int) (unsigned long long) but then some unix compilers
are complaining about this construct (using gcc 3.3+).

-- Henrik


 
Reply With Quote
 
Henrik Goldman
Guest
Posts: n/a
 
      11-28-2006
>
> void fn(void* pSettings)
> {
> int* pInt = (int*)pSettings;
> // Use pInt here
> }
>


Would this not cause new problems?
Are you talking about using pInt directly as an integer? Otherwise the users
would change:

fn(123);

to:
int dummy = 123;
fn(&dummy);

This would not exactly be for the better since it would go against
simplicity.

-- Henrik


 
Reply With Quote
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      11-28-2006
On 2006-11-28 21:48, Henrik Goldman wrote:
>>
>> void fn(void* pSettings)
>> {
>> int* pInt = (int*)pSettings;
>> // Use pInt here
>> }
>>

>
> Would this not cause new problems?
> Are you talking about using pInt directly as an integer? Otherwise the users
> would change:
>
> fn(123);
>
> to:
> int dummy = 123;
> fn(&dummy);
>
> This would not exactly be for the better since it would go against
> simplicity.


Create the copy in the function then:

void fn(void* pSettings)
{
int Int = *((int*)pSettings);
// Use Int here
}

Of course users would still have to use the address-of operator when
pasing arguments that are not pointers.

--
Erik Wikström
 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      11-28-2006

Henrik Goldman napsal:
> >
> > void fn(void* pSettings)
> > {
> > int* pInt = (int*)pSettings;
> > // Use pInt here
> > }
> >

>
> Would this not cause new problems?
> Are you talking about using pInt directly as an integer? Otherwise the users
> would change:
>
> fn(123);


This shouldn't be possible (123 is not pointer). There may be problem
with 0 (zero), because it is also valid pointer value in C++, but I do
not see there big problem.

>
> to:
> int dummy = 123;
> fn(&dummy);


Yes, that's the way how I meant it.

 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      11-28-2006

Henrik Goldman napsal:
> >
> > void fn(void* pSettings)
> > {
> > int* pInt = (int*)pSettings;
> > // Use pInt here
> > }
> >

>
> Would this not cause new problems?
> Are you talking about using pInt directly as an integer? Otherwise the users
> would change:
>
> fn(123);
>
> to:
> int dummy = 123;
> fn(&dummy);
>
> This would not exactly be for the better since it would go against
> simplicity.
>
> -- Henrik


You can provide to user set of functions with correct parameters and
then call this one generic function, which is declared as static, so
user's cannot call it from other source files.

If you need some "variant" type with C interface, you can also use
union.

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      11-29-2006
On Tue, 28 Nov 2006 16:19:40 +0100 in comp.lang.c++, "Henrik Goldman"
<> wrote,
>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.


Overloading of functions in this situation gets you nothing more than
cleaner notation. The same thing can be accomplished with C
compatibility with a set of functions named
f1_int(int);
f1_double(double);
etc. This is basically what some C++ compilers do to implement
overloaded functions anyway, with "name mangling" for the linker.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Portable Python - free portable development environment ! perica.zivkovic@gmail.com Python 7 01-13-2007 11:19 AM
portable (VHDL) vs. non-portable (altera LPM) approaches to signed computations Eli Bendersky VHDL 1 03-01-2006 02:43 PM
Re: typecast error! Mark Fitzpatrick ASP .Net 1 02-08-2006 04:45 AM
Re: typecast error! Otis Mukinfus ASP .Net 1 02-04-2006 05:32 PM
Re: typecast error! Joe Van Meer ASP .Net 1 02-02-2006 02:00 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57