cast from void* to int

S

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.
 
M

Markus Schoder

san said:
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);
 
K

Kai-Uwe Bux

san said:
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
 
S

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.
 
R

Rolf Magnus

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top