364,036 Members | 5023 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

cast from void* to int

san
P: n/a
san
Hi,

I need to cast a void* to int.
I am doing the following

int sig = reinterpret_cast<int> (clientData);
where clientData is of type void*.

Its working well for 32-bit machine.
But giving error on 64-bit as
"Error: Cannot cast from void* to int."
What could be the reason..
Please help.

Thanks.

Apr 20 '06 #1
Share this Question
Share on Google+
5 Replies


Markus Schoder
P: n/a
Markus Schoder
san wrote:[color=blue]
> Hi,
>
> I need to cast a void* to int.
> I am doing the following
>
> int sig = reinterpret_cast<int> (clientData);
> where clientData is of type void*.
>
> Its working well for 32-bit machine.
> But giving error on 64-bit as
> "Error: Cannot cast from void* to int."
> What could be the reason..
> Please help.[/color]

On a 64-bit machine void * is likely to be a 64-bit entity while an int
is probably still only 32-bit so your compiler refuses to do this
because the pointer would get truncated making it impossible to ever
get it back from the int.

If you _really_ do not care about truncation you could try

long long sig1 = reinterpret_cast<long long> (clientData);
int sig = static_cast<int>(sig1);

Apr 20 '06 #2

Kai-Uwe Bux
P: n/a
Kai-Uwe Bux
san wrote:
[color=blue]
> Hi,
>
> I need to cast a void* to int.
> I am doing the following
>
> int sig = reinterpret_cast<int> (clientData);
> where clientData is of type void*.
>
> Its working well for 32-bit machine.
> But giving error on 64-bit as
> "Error: Cannot cast from void* to int."
> What could be the reason..[/color]

What is the output of:

#include <iostream>

int main ( void ) {
std::cout << "sizeof(int) = " << sizeof(int) << '\n';
std::cout << "sizeof(void*) = " << sizeof(void*) << '\n';
}



Best

Kai-Uwe Bux
Apr 20 '06 #3

san
P: n/a
san
sizeof(int) is 32 while sizeof(void*) is 64.

long long sig1 = reinterpret_cast<long long> (clientData);
int sig = static_cast<int>(sig1);

It worked well.
I dont worry about the truncation because what I get is only a no from
1 to 48.

Thanks a lot.

Apr 20 '06 #4

Ian Collins
P: n/a
Ian Collins
san wrote:[color=blue]
> sizeof(int) is 32 while sizeof(void*) is 64.
>[/color]
You can probably just use a long, check its size.

--
Ian Collins.
Apr 20 '06 #5

Rolf Magnus
P: n/a
Rolf Magnus
san wrote:
[color=blue]
> Hi,
>
> I need to cast a void* to int.[/color]

Why?
[color=blue]
> I am doing the following
>
> int sig = reinterpret_cast<int> (clientData);
> where clientData is of type void*.
>
> Its working well for 32-bit machine.
> But giving error on 64-bit as
> "Error: Cannot cast from void* to int."
> What could be the reason..[/color]

The reason could be that an int is not suitable to take the value of a
pointer.

Apr 20 '06 #6

Post your reply

Help answer this question



Didn't find the answer to your C / C++ question?

You can also browse similar questions: C / C++