Skip to main content
GameDev.net gamedev.net
🔒 Locked

pointers and integers

Started by CProgrammer Jun 18, 2007 at 2:40 PM 6 replies 700+ views
Original Post
CProgrammer
CProgrammer
Hi, im trying to find an elegant way to convert a pointer into an integer and later back into a pointer. I used explicit casting, however this causes the compiler to give me a warning stating that the types have different sizes. Well they are both 4 bytes but nonetheless on another machine this may not be the case. Is there a way to perhaps create a general function. Seeing as I cant use arithmetic functions on pointers this seems hard. -CProgrammer
rip-off
rip-off
Why do you need to do this?

There is probably a far better way of achieving your goal.

Making a pointer from an integer is always an extremely dangerous idea.
ToohrVyk
ToohrVyk
  1. Converting pointers to integers is not elegant, no matter how you do it.

  2. The reverse conversion is implementation-defined, so unportable by definition. Converting to integer, altering the integer, and converting back, is even less portable.

  3. Why do you need an integer representation of a pointer?
CProgrammer
CProgrammer
The system I am developing for only supports transfering ints and bytes. Theres no alternative.
Anyone know what a good way of doing it is.

-CProgrammer
Anon Mike
Anon Mike
Quote:
Original post by CProgrammer
im trying to find an elegant way to convert a pointer into an integer and later back into a pointer. I used explicit casting, however this causes the compiler to give me a warning stating that the types have different sizes. Well they are both 4 bytes but nonetheless on another machine this may not be the case.


I'm guessing you're using VC and have the "Detect 64-bit Portability Issue" option on. The compiler is trying to tell you that doing such things is iffy at best and will definitely break on other platforms.

The easiest "fix" is to turn off the option: Project->Properties->C/C++.

Another option if you don't mind being Windows-only is to use the helper functions "PtrToUint" and "UintToPtr" (see basetsd.h).
-Mike
ToohrVyk
ToohrVyk
Quote:
Original post by CProgrammer
The system I am developing for only supports transfering ints and bytes. Theres no alternative.


What system, and transferring from where to where?

Quote:
Anyone know what a good way of doing it is.


As mentioned above, the result of int → pointer conversion depends on your operating system, your hardware and your compiler—which ones are you using?

Spoonbender
Spoonbender
Quote:
Original post by CProgrammer
The system I am developing for only supports transfering ints and bytes. Theres no alternative.
Anyone know what a good way of doing it is.

-CProgrammer


transferring how and from where to where? (It rarely makes sense to transfer pointers. Memory addresses tend to be machine-specific)
joanusdmentia
joanusdmentia
I believe the type std::size_t is guarentteed to be large enough to store a pointer (and VS2005 doesn't bitch about it with warning level 4 and 64-bit compatibility warnings enabled).
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.