subtracting two pointers

tll at druxu.UUCP tll at druxu.UUCP
Wed Jan 25 09:01:12 AEST 1984


>From an article by Liudvikas Bukys:

>> One possible outcome of this "difference of two pointers can't fit in
>> an int or even a long int" debate is that the difference will be
>> defined to be implementation-dependent (close to undefined, except that
>> op sys hackers can know what's going on) if the pointers do not
>> reference elements of the same array.  Maybe any array will be limited
>> to having at most MAXINT elements.

The difference is already at least somewhat implementation-dependent.  I
quote from "The C Reference Manual" page 189:

   This conversion will in general give unexpected results unless the
   pointers point to objects in the same array, since pointers, even to
   objects of the same type, do not necessarily differ by a multiple of
   the object-length.

So we've already been warned not to subtract pointers to objects that
are not in the same array (albeit for a reason other than integer
overflow).  It appears that limiting the maximum number of elements in
an array is all that is needed to finish clarifying things.  On the
other hand, this limit need not be enforced, since the programmer knows
that conversion to an "int" causes truncation if the value won't fit.
So what's the big problem, anyway?

			Tom Laidig
			AT&T Information Systems Laboratories, Denver
			...!ihnp4!druxu!tll



More information about the Comp.lang.c mailing list