Re: far pointer



>> >> > In C we can compare two pointers for equality,
>> >> > and if they point to the same address they will compare as equal.
>> >> > However there is another restriction,
>> >> > which is that pointers must be
>> >> > calculated within the object
>> >> > (array, to all intents and purposes) to which
>> >> > they point.
>> >> > If you play silly games with absolute addresses or wild
>> >> > calculations, you can defeat C's pointer representation,
>> >> > and it is posible
>> >> > that two pointers may point to the same location,
>> >> > but have different bit
>> >> > representations, and not compare as equal.
>
>If two pointers point to the same location,
>they will compare equal regardless of whether or not
>the bit patterns are the same.

If two pointers *that you could create without invoking undefined
behavior* point to the same location, they will compare equal
regardless of whether or not the bit patterns are the same.

That means that if you are working with a x86 machine in real mode,
with 16-bit segment and 16-bit offset, and you're allowing objects
larger than 64k, that you (the implementor) have to do somewhat
complicated and slow code to do pointer math, and you probably have
to normalize two pointers as part of comparing them for equality.

If, on the other hand, you do NOT allow objects larger than 65535
bytes, the implementation can just do simple pointer math (involving
only the offset), and as long as the implementation doesn't allow
a legitimate way to GET pointers to the same object with different
segment register contents, and comparison can just do a bit-by-bit
comparison (although for equality it still has to compare 32 bits.

>> If you want to compare two pointer values using "<", "<=", ">", or
>> ">=", both pointers must point into the same object (or just past the
>> end of it). If you want to compare two pointer values using "=" or
>> "!=", they just both have to be valid, i.e., each has to point into
>> *some* object, not necessarily the same one (or just past the end of
>> some object, and there's a special case allowing two objects to be
>> adjacent).

Gordon L. Burditt
.



Relevant Pages

  • Re: storage for temporaries
    ... >> Netocrat wrote: ... >> allegedly have no pointers to ojbects!). ... I suppose it is unclear how to compare ... pointers on a system that doesn't have a flat memory model. ...
    (comp.lang.c)
  • Re: Pointer equality and dereferencing
    ... a representation representing two different values. ... If a pointer to an object and a pointer past an array compare equal, ... a different array object that happens to immediately follow the first ... the attempt to verify that the pointers happen to have the ...
    (comp.std.c)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... The standard form is like this, ... 1108 The sizeof operator ... ... being able to compare unrelated pointers, ... So long as the pointers are of the same type, ...
    (comp.lang.c)
  • Re: STL algorithms & operator overloading
    ... > I'm trying to sort vector of pointers to classes using STL ... do is to write a suitable function to compare two pointers to object by ...
    (alt.comp.lang.learn.c-cpp)
  • Re: far pointer
    ... Gordon Burditt wrote: ... >>they will compare equal regardless of whether or not ... > If two pointers *that you could create without invoking undefined ... > regardless of whether or not the bit patterns are the same. ...
    (comp.lang.c)