Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

addr to long

0 views
Skip to first unread message

Krzysztof Poc

unread,
Apr 30, 2009, 6:42:15 AM4/30/09
to
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

pau...@mbnet.fi

unread,
Apr 30, 2009, 6:52:46 AM4/30/09
to
On 30 huhti, 13:42, Krzysztof Poc <fajfu...@wp.pl> wrote:
> If not, what is correct ?

Not converting a pointer.

Juha Nieminen

unread,
Apr 30, 2009, 7:05:20 AM4/30/09
to
Krzysztof Poc wrote:
> 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.)

blargg

unread,
Apr 30, 2009, 4:57:40 PM4/30/09
to

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.

SaticCaster

unread,
May 1, 2009, 4:28:31 AM5/1/09
to
> long addr = reinterpret_cast<long>(&objOfAnyType);
> Is it correct for all platforms 32/64 bit ?

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

James Kanze

unread,
May 1, 2009, 6:18:37 AM5/1/09
to
On Apr 30, 1:05 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Krzysztof Poc wrote:

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

0 new messages