473,657 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cast from void* to int

san
Hi,

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

int sig = reinterpret_cas t<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 39268
san wrote:
Hi,

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

int sig = reinterpret_cas t<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_cas t<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_cas t<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(voi d*) = " << 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_cas t<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_cas t<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
2431
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 CImplementation(); pInterface->Method(); Case 2:
5
8717
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 use it with. Now, if that T happens to be some subclass of a known base class (object, in this case), I want to perform some extra stuff ... I've read Faq#35, and the most natural solution would have
16
17263
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
3513
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 accepts a void* key argument, and uses the functions above to store this argument. It returns something (linked to the key) that the caller can store a value in. The actual key argument is always of the same pointer type (as seen in the caller,...
31
3838
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
2713
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: 0x00000000 through 0xFFFFFFFE : Valid byte addresses
18
10402
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
2869
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 of non- working code: CvMat *rowVect = cvCreateMat(1,nfeatures,CV_MAKETYPE(images- //rowVect is a pointer to CvMat type cvReshape( images, rowVect, 0, 1 ); //vectorize the image; readrow is a 1xN matrix(vector)
7
3945
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
1662
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 void *pVoid = new C(); C *pC = dynamic_cast<C *(pVoid);
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.