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
Not converting a pointer.
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.)
Depends on what you define as correct. Why are you converting a pointer to
a long? Do you want to examine bits, or later convert it back to the
original pointer value? If the latter, you'll have to use a non-portable
approach. If you've got <stdint.h>, you can use (u)intptr_t; if not, the
larger of long and size_t (ptrdiff_t if you really want it signed) is your
best bet.
ILP32 data model: correct
LP64 data model (64-bit xNix): correct
LLP64 data model (Win64): NOT correct
http://www.viva64.com/terminology/Data_model.html
> If not, what is correct ?
I am recommend use ptrdiff_t or size_t.
http://www.viva64.com/art-1-2-1756520624.html
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.)
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34