Sizes, alignments, and maxima (was: Contiguous Arrays)

Art Boyne boyne at hplvli.HP.COM
Fri Feb 24 01:29:11 AEST 1989


badri at valhalla.ee.rochester.edu (Badri Lokanathan) writes:
>For instance, if the array was to be x[101] to x[110] instead of
>x[0] to x[9], what is the easiest way to do it?
>For automatic arrays one could do
>int space[10], *x; 
>x = &space[0] - 101;
>
>In article <1989Feb22.171441.7957 at utzoo.uucp>, henry at utzoo.uucp (Henry Spencer) writes:
>> No one couldn't, not if one wants to be portable.  There is absolutely no
>> guarantee that x[101] will be the same as space[0], and in fact there is
>> no guarantee that the computation of x won't trap and give you a core dump.
                         ^^^^^^^^^^^
>> On a sufficiently odd segmented machine it might.  Even K&R1 warns you that
>> pointer arithmetic won't necessarily do what you think unless you keep it
>> within an array.
>
>Henry is probably referring to the comments on pages 98 and 189 of K&R1.
>My argument is, since x is an independent variable (a pointer, but
>still a variable,) there is no way of checking any array bounds.
>Thus in order for consistency with the definition of pointer
>arithmetic,
>x -= i; x += i;
>should result in the original value of x (provided the resulting
>intermediate values are within pointer ranges.)
>Since x can be initialized to any value, this should always be possible,
>the result after each operation being an address appropriately offset
>from the original object. Yes, accessing the contents of an illegal location
                                ^^^^^^^^^
>will almost certainly screw things up, but there is no access happening.

Sorry, but read Henry's response more carefully:  it is possible that the
*computation* of x-=i will cause an abort/trap/etc - without attempting
any *access*.  This is especially likely if x=-i would result in x pointing
to a negative-index array location (consider a machine whose address
registers trapped on a negative segment offset and whose compiler put
the array into a separate segment, perhaps due to segment size limitations).  
Henry is correct: pointer arithmetice isn't guaranteed unless you keep it
*within* an array.

Art Boyne, boyne at hplvla.HP.COM



More information about the Comp.lang.c mailing list