By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,805 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

cast from void* to int

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


P: n/a
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

P: n/a
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

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

P: n/a
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

P: n/a
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.