addr to long

K

Krzysztof Poc

Hello

How can I convert ptr (of any type) to long.
Can I do that as follows:

long addr = reinterpret_cast<long>(&objOfAnyType);

Is it correct for all platforms 32/64 bit ?
If not, what is correct ?

thanks for help
 
J

Juha Nieminen

Krzysztof said:
Hello

How can I convert ptr (of any type) to long.
Can I do that as follows:

long addr = reinterpret_cast<long>(&objOfAnyType);

Is it correct for all platforms 32/64 bit ?

The standard does not guarantee that sizeof(long) equals sizeof(void*)
(and in fact, in MSVC long is 32-bit and void* is 64-bit when compiling
a 64-bit binary).

If you want a signed integral which is as large as a pointer, use the
ptrdiff_t standard type. (If you want it unsigned, use size_t.)
 
J

James Kanze

Krzysztof Poc wrote:
The standard does not guarantee that sizeof(long) equals
sizeof(void*) (and in fact, in MSVC long is 32-bit and void*
is 64-bit when compiling a 64-bit binary).
If you want a signed integral which is as large as a pointer,
use the ptrdiff_t standard type. (If you want it unsigned, use
size_t.)

There's no guarantee that ptrdiff_t or size_t are large enough
either. (I've used systems where they weren't.)

If you have C99 or C++0x, you can use intptr_t or uintptr_t.
(Theoretically, they're not guaranteed to exist. But in
practice, you're guaranteed an integral type of at least 64
bits, and I don't see 64 bits not sufficing anytime in the near
future---you can address a lot of memory with 64 bits.)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top