When is a cast not a cast?

Hamish Reid hamish at unisoft.UUCP
Fri May 19 03:52:00 AEST 1989


In article <2890 at buengc.BU.EDU> bph at buengc.bu.edu (Blair P. Houghton) writes
about adding two pointers in C, along with the amusingly gratuitous:
	"Ask yer second-grade teacher, or get another career"
addressed to Doug Gwynn....
 
Blair, you still haven't answered the question.

What does it mean in C to add two pointers (or locations? Two
addresses?) And what is the type of the result? And why would you
seriously want to try it anyway? (Don't assume any particular
implementation of pointers - it's deliberately opaque - so non-reasons
like loop effciency are out - I can understand that under
severaly-restricted conditions where all implementation details are
well-understood that it occasionally makes some sort of sense - but
we're talking C here, not "brand X's C on machine Y under operating
system Z").

Think about it all slightly differently. ***!!Dangerous Analogy Ahead -
Proceed With Caution!!*** Let's try a similar thing - let's try to add
two street locations. What does it mean to add (say) 2052 McGee Ave to
2062 McGee? Or even to subtract 2052 McGee from 2062 (or, *horror!!*,
the reverse...). You can even assume that they are in the same city
block (though in real life they're not :-)).

What does the result *mean*? In C terms, what is the type of the
result? Is it an address? Or is it actual apartment objects? Or
what...? Adding the two street addresses is a poorly-defined operation;
you can make it well-defined if you like, using formal methods, and
redefining the semantics/types/etc of street addresses, etc, but the
resulting semantics will contradict our intuitive ideas about these
things. Fine, if you like that sort of thing. Don't try the same with C
unless there's some real point, and it is a well-understood change.

Dangerous Analogy, again - it makes some sort of sense to add (say) "8"
(units unspecified but generally understood) to 2052 McGee; in some
reasonably-well understood intuitive sense it probably means something
like move 8 apartments (note type and implicit scaling here) to the
south down McGee, and read the address - "2060 McGee" (actually, the
wrong answer - it's not the same block - but we can allow for that if
we know the particular implementation...).  This is *not* the same as
adding two actual addresses.

Blair, a constructive challenge:

Post, to this newsgroup, a formal semantics definition of some proposed
changes to C that would allow you to happily and meaningfully add C
pointers as they are currently defined. You can use any reasonable
formal semantics (my preference would be some form of Plotkin- or
Milner-esque operational semantics (a la the LFCS stuff), or Natural
Semantics, or if you have to, some sort of denotational semantics), and
a small amount of handwaving, but it had better formally address the
types of the operands and the type of the result; and show how it is
compatible with the rest of C's semantics. I think *you'll* find it
difficult, but I'm definitely *not* saying it can't be done. When
you've done that, you can go off and tell Doug Gwynn to get a new
career or ask his second grade teacher....

Until we see this formal semantics, many of us will remain convinced
that you still do not have a totally clear grasp of pointers - or C for
that matter. And apologies to those who notice these things, for any
looses analogies or handwaving above, but I think this discussion just
seems to revolve round and round and I'm getting careless....

Cheers,
	Hamish
-----------------------------------------------------------------------------
Hamish Reid             UniSoft Corp, 6121 Hollis St, Emeryville CA 94608 USA
+1-415-420-6400         hamish at unisoft.com,         ...!uunet!unisoft!hamish



More information about the Comp.lang.c mailing list