Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!accuvax.nwu.edu!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: When is a cast not a cast? Message-ID: <17634@mimsy.UUCP> Date: 20 May 89 05:30:57 GMT References: <2747@buengc.BU.EDU> <10191@smoke.BRL.MIL> <406@skye.ed.ac.uk> <2906@buengc.BU.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 81 >>In article <2890@buengc.BU.EDU> bph@buengc.BU.EDU (Blair P. Houghton) presented an >[argument for allowing pointer addition so we can hand-optimize a loop] (his paraphrase). >In article <17569@mimsy.UUCP> I commented: >>I cannot think of a realistic example of this. In article <2906@buengc.BU.EDU> bph@buengc.BU.EDU (Blair P. Houghton) writes: >And I don't know why we need arrays at all...or Pascal, for that matter. >:-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) Actually, I was wishing you had provided one so I could shoot it down. :-) (No fair making up straw men....) Back to me: >>Anyway, consider another argument against pointer+pointer addition: >>Suppose we have a segmented architecture, .... >You're using an architecture to specify C. Is that not a no-no? It might be, but that is not what I did. A better analogy is found in mathematics: To prove a statement, you must show that it is always true. To disprove it, a single counterexample suffices. [much deleted] >Further, the examples you give are not limited to pointer+pointer >arithmetic. They'd result in over/underflow even if done in >proper pointer+int form. Not so. (My `examples' were only to describe the behaviour of a hypothetical architecture; I did not provide an example of pointer addition.) In any case, if you will consider for a bit you can come up values for the following: pointer_type p, q, r; integral_type pqdiff; r = (p + q) / 2; /* p+q overflows */ r = p + (q - p) / 2; /* no subexpression overflows */ >The same would be true if we were trying to add unsigned integers >that weren't pointers at all. No; unsigned arithemetic is modular arithmetic. (It is also quite slow on some architectures.) >Or a couple of really big floats... It is true that in double a, b, avg; ... avg = (a + b) / 2.0; the computation of (a+b) might overflow. To avoid this, a careful programmer will use avg = a + (b - a) / 2.0; where it matters. The key difference between this and the pointer version is that one can portably write #define SAFE 10000.0 /* see pANS for a larger safe value */ if (a < SAFE && b < SAFE) avg = (a + b) / 2.0; else avg = a + (b - a) / 2.0; whereas there is no way to portably write if (p < ??? && q < ???) /* cannot fill in ??? portably */ avg_of_p_and_q = (p + q) / 2; else avg_of_p_and_q = p + (q - p) / 2; (as I mentioned in an article just posted moments ago). -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris