In article <42***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
grocery_stocker wrote:
How come you can subtract pointers, but you cannot add them?
What would such a construct mean?
"127 Main Street" minus "123 Main Street" equals "2 houses".
Depends. On my street, houses are generally 4 apart numerically.
555 is next to 559.
"127 Main Street" plus "123 Main Street" equals ?????.
When 123 is adjacent to 127, then the result of the addition
would be an apartment block ;-)
One could potentially develop the idea that "adding" pointers
could have the effect of creating a composite object. That is,
current C standards say that any given pointer is allowed to
move through a single object, and the pointer may assume
a value which points immediately after the object (as long
as said pointer is not dereferenced), but setting the pointer
before the object or past 1-after the object is not valid.
In a hypothetical C-like language that allowed "addition" of
pointers, the result might be something that was allowed to
move through the two objects.
[On the flat memory model where pointers are just indices into a block
of virtual memory, this might not make much sense, but since
pointers to different types don't have to be in the same address
space [as long as void* conversions work], a composite-pointer
might start to mean something. Even in the pointer- is-
virtual index case, not all the virtual address space is
necessarily populated, so it might mean something there too.
The only thing I can think of where such a thing might actually
be -useful- has to do with the fact that in C, you are only
allowed to compare pointers if they point to the same object
(or no further than 1 past the object). It is, though, sometimes
useful to be able to compare pointers more generally -- e.g.,
in linked lists, if you want to compare a node pointer to
the list head pointer, then it's certainly not fun to
contemplate that the comparision result is undefined
if the two nodes were allocated in different malloc() calls...
--
Any sufficiently old bug becomes a feature.