Topic: Comparison of function pointers for in


Author: Stephen.Clamage@eng.sun.com (Steve Clamage)
Date: 1997/02/22
Raw View
In article 2002972127020001@christian-mac.isltd.insignia.com, christian.bau@isltd.insignia.com (Christian Bau) writes:
>>
>> Consider:
>>
>>     bool
>>     compare( void (*p1)() , void (*p2)() )
>>     {
>>         return p1 < p2 ;
>>     }
>>
>
>May I add another question to this: Why does comparing unrelated pointers
>result in undefined behavior? (I know there is no reason why in this
>example p1 should be less than p2, but it would not be unreasonable that
>for any two pointers that are not equal, either p1 < p2 or p1 > p2 should
>always be true).

You can compare two pointers of compatible type for equality (or
inequality). You can also compare two pointers into the same object or
array (or one item off the end of an array) for greater-than/less-than.

If you compare pointers for greater-than/less-than but they do not point
into the same object or array, the results are unspecified (not undefined).
The assumption that one pointer must be either greater than, less than, or
equal to the other is not valid.

Consider also systems that use handles for pointers. The actual objects
can move around during the course of program execution. The ordering of
the handles tells you nothing about the ordering of the objects, and
indeed the ordering of the objects could change.

---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]