Re: ptr1 == ptr2 but (int)ptr1 != (int)ptr2
- From: Kenneth Brody <kenbrody@xxxxxxxxxxx>
- Date: Fri, 25 May 2007 13:20:54 -0400
Hallvard B Furuseth wrote:
Do anyone know of an architecture where this can break?
T *ptr1, *ptr2;
...
if (ptr1 == ptr2)
if (CHAR_BIT*sizeof(T*) <= (width of int)) /*otherwise undefined*/
assert((int)ptr1 == (int)ptr2);
(Feel free to replace int with another integer type if that helps to
break something.)
I know an addess can have several representations at least on some DOS
memory models, but I don't know if it normalizes pointers before
converting to integer.
I would suspect that, on most platforms in which a pointer can fit
into an int, if the pointers compare equal, then the converted-to-int
values will also compare equal.
However, I'm sure the standard probably says it's implementation
defined at best.
Consider a segmented memory architecture, such as "real-mode" on
the x86 line of processors. On such platforms "far" pointers are
32 bits (16-bit segment, plus 16-bit offset), and I would suspect
that it may be possible for two pointers to compare equal, even
if their bit patterns are not identical. (For example, it may
compare FFFF:0000 and F000:FFF0 as "equal", but 0xFFFF0000 and
0xF000FFF0 as ints [or perhaps longs] will not compare equal.)
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@xxxxxxxxx>
.
- References:
- ptr1 == ptr2 but (int)ptr1 != (int)ptr2
- From: Hallvard B Furuseth
- ptr1 == ptr2 but (int)ptr1 != (int)ptr2
- Prev by Date: Re: sound_C_under_linux
- Next by Date: Re: Use of memcpy() to transfer from memory to a variable
- Previous by thread: ptr1 == ptr2 but (int)ptr1 != (int)ptr2
- Next by thread: Re: ptr1 == ptr2 but (int)ptr1 != (int)ptr2
- Index(es):
Relevant Pages
|