[comp.lang.c] comp.std.c

lhf@aries5 (Luiz H. deFigueiredo) (08/31/89)

There has been some discussion on the net about hardware protection and
out-of-bounds pointers.

ANSI C says that is *is* legal to use mention (but not dereference) a pointer
just out-of-bounds as in

	char a[N];
	char *last=a+N;		/* Here! */
	char *p;

	for (p=a; p<last; p++)
		do something;

Now I ask, it is possible/legal to do the analogous thing for a-1 as in

	char *head=a-1;		/* Here! */

	for (p=last-1; p>head; p--)
		do something else;

-------------------------------------------------------------------------------
Luiz Henrique de Figueiredo		internet: lhf@aries5.waterloo.edu
Computer Systems Group			bitnet:   lhf@watcsg
University of Waterloo     (possible domains are waterloo.edu and uwaterloo.ca)
-------------------------------------------------------------------------------
eof

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/31/89)

In article <426@maytag.waterloo.edu> lhf@aries5 (Luiz H. deFigueiredo) writes:
>Now I ask, it is possible/legal to do the analogous thing for a-1 as in
>	char *head=a-1;		/* Here! */

The Standard does not guarantee that this will work, although it might work
uner some circumstances under some implementations.

The asymmetry between last+1 (guaranteed) and first-1 (not guaranteed) is
due to the former only requiring one byte safety allowance while the latter
requires an entire array member amount of safety allowance.  It was
considered sufficient to guarantee just one way of traversing an array,
and the readily implementable choice was made.

andre@targon.UUCP (andre) (09/01/89)

In article <10884@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes:
-In article <426@maytag.waterloo.edu> lhf@aries5 (Luiz H. deFigueiredo) writes:
->Now I ask, it is possible/legal to do the analogous thing for a-1 as in
->      char *head=a-1;         /* Here! */
-
-The Standard does not guarantee that this will work, although it might work
-uner some circumstances under some implementations.
-
-The asymmetry between last+1 (guaranteed) and first-1 (not guaranteed) is

Does this mean that on OS/2 on an '386 with a buffer gotten from
the system, the compiler sees to it that the char *head does never!
ends up in an address register? Or else the '386 will give an exeption
(also if you never dereference the register, or segment - offset that
make up the 'address register').

-- 
    \---|    AAA         DDDD  It's not the kill, but the thrill of the chase.
     \  |   AA AAvv   vvDD  DD        Ketchup is a vegetable.
  /\  \ |  AAAAAAAvv vvDD  DD                    {nixbur|nixtor}!adalen.via
_/__\__\| AAA   AAAvvvDDDDDD    Andre van Dalen, uunet!hp4nl!targon!andre

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/03/89)

In article <619@targon.UUCP> andre@targon.UUCP (andre) writes:
>Does this mean that on OS/2 on an '386 with a buffer gotten from
>the system, the compiler sees to it that the char *head does never!
>ends up in an address register?

I'm not familiar enough with that environment to answer definitively,
but I don't understand the problem.  The whole reason the Standard
does not guarantee array[0]-1 is to accommodate such address space issues.
The reason it does guarantee array[last]+1 is because otherwise looping
would be painful; since only a single byte (or maybe word) extra is
required at the end, the compiler should arrange for the extra slop
and all will be well.