On Tue, Jul 12, 2005 at 11:42:23PM +0200, Erik Trulsson wrote:
> Pointer subtraction is only well defined if both pointers point to elements
> in the same array (or one past the end of the array).  Otherwise the
> behaviour is undefined.

While this is correct, there are certain cases that the standard leaves
undefined but that nevertheless can be useful; for example, pointer
subtraction can be used to estimate the amount of stack in use (of
course, it is necessary to know if the stack grows upward or downward).

Similarly, in an OS kernel, comparing pointers may make sense.

> If the compiler is certain that the pointers do not point into the same
> array or structure (as in my first example above) it is probably a good
> idea to give a warning, but it should not warn for the legal cases (as in my
> second example.)

I'd say that would be only useful in terms of a more general analysis that
implements bounds checking.  Given reliable analysis that finds suspicious
pointer comparisons, most users would be more interested in the implicit
comparison of a pointer with the boundaries of the array it corresponds
to.


Reply via email to