473,414 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 39184
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
16
by: Burne C | last post by:
I have something confused in my mind void *search_address = 0; ++search_address; /* The compiler complain that "void *" is unknown size. OK, I changed the code
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
31
by: Twister | last post by:
Hi All, I have a question which might sound very basic. I have a simple structure: struct simple{ void *buffer; }; typedef struct simple Simple;
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
18
by: planetzoom | last post by:
Given the following code: #include <stdio.h> int main(void) { char array = "What is your favorite car?"; void *vp = &array; printf("%s\n", vp);
10
by: kkirtac | last post by:
Hi, i have a void pointer and i cast it to an appropriate known type before using. The types which i cast this void* to are, from the Intel's open source computer vision library. Here is my piece...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
6
by: puzzlecracker | last post by:
What about dynamic_cast when dynamic reference is not unique (e.g. A- Why would you use dynamic cast<void *>? What is typeid & what does it return? How would you use it? What is wrong here ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.