469,322 Members | 1,973 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,322 developers. It's quick & easy.

cast from void* to int

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
5 37404
san wrote:
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.


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
san wrote:
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..


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
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
san wrote:
sizeof(int) is 32 while sizeof(void*) is 64.

You can probably just use a long, check its size.

--
Ian Collins.
Apr 20 '06 #5
san wrote:
Hi,

I need to cast a void* to int.
Why?
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..


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

Apr 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by He Shiming | last post: by
16 posts views Thread by Burne C | last post: by
5 posts views Thread by Stijn van Dongen | last post: by
31 posts views Thread by Twister | last post: by
9 posts views Thread by Frederick Gotham | last post: by
18 posts views Thread by planetzoom | last post: by
10 posts views Thread by kkirtac | last post: by
7 posts views Thread by * Tong * | last post: by
6 posts views Thread by puzzlecracker | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.