Out of range pointers

Richard Harter g-rh at cca.CCA.COM
Sun Sep 18 08:54:21 AEST 1988


In article <8515 at smoke.ARPA> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:

Re comments about x[-1] should be legal and should be in the standard.

>I doubt that any effect on the computer industry would have occurred
>other than reduced adherence to the postulated C standard.  People
>writing portable applications would still not be able to compute
>&array[-1], since several compilers would ignore that requirement
>(benchmark speed is a far greater driving factor than the desires of
>a few sloppy programmers to compute non-existent addresses).  What
>good would that situation accomplish?  Better that the standard be
>widely followed and that programmers become better educated about
>actual portability considerations, than to encourage false hopes for
>availability of features that are difficult or detrimental to provide.

You may be right about reduced adherence, at least in this regard.
However the problem is not simply a matter of "sloppy" programming.
In C a pointer is a fairly anonymous object.  What you are saying is
that it is a potential error to add or subtract an integer from a
pointer if the result is out of range.  Very well, but what is that
range?  Suppose a pointer is passed through a calling sequence.  In
the function I have no way of knowing whether &x[n] will break for any
n other than 0.  For that matter I have no way of knowing whether 
x is a legal pointer!

In principle this is not right -- there is no way to write defensive
code to check on pointer validity.  To be sure a "correct" program
never has an invalid pointer and all that but what about the rest of
us poor mortals?
-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.



More information about the Comp.lang.c mailing list