Re: ptr1 == ptr2 but (int)ptr1 != (int)ptr2



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>

.



Relevant Pages

  • ptr1 == ptr2 but (int)ptr1 != (int)ptr2
    ... T *ptr1, *ptr2; ... (Feel free to replace int with another integer type if that helps to ...
    (comp.lang.c)
  • Re: what does this mean ?
    ... int main ... Test *ptr1 = new Test; ... delete ptr2; ... courtesy of default initialization. ...
    (microsoft.public.vc.language)
  • Re: pointer arithmetic
    ... int *Ptr1, *Ptr2; ... houses on your side of the street are odd-numbered, ...
    (comp.lang.c)
  • Re: far pointer
    ... In C we can compare two pointers for equality, and if they point to the same ... int *ptr1 = x; ...
    (comp.lang.c)
  • Re: what does this mean ?
    ... int main ... Test *ptr1 = new Test; ... delete ptr2; ...
    (microsoft.public.vc.language)