array[-1] -- permitted?

carroll at s.cs.uiuc.edu carroll at s.cs.uiuc.edu
Sat Sep 24 02:33:00 AEST 1988


/* Written  1:42 am  Sep 22, 1988 by kyriazis at rpics in s.cs.uiuc.edu:comp.lang.c */
Excuse for the question, but that is the first time I am looking at that
subject and I don't see any reason why x[-a] can't be permitted (...)
Am I missing something?
  kyriazis at turing.cs.rpi.edu
------------------------------
/* End of text from s.cs.uiuc.edu:comp.lang.c */

The problem arises on machines with non-linear address spaces. The classic
example is the 80x86 family from Intel. Address on this machine consist
of two parts - the segment and offset. For large arrays, it is handy
to have the array start at the beginning of segment, and make the segment
large enough to hold the array. Therefore, array arithmetic is done only
with the offset, making the code run much faster. Unfortunately, the
price you pay is that negative absolute indices 'wrap around' the segment,
and give bizarre results. This can be finessed in YACC by declaring
the array and a pointer, and then if YACC has a 'largest' negative index
of 1 or 2, set pointer = array + 1 or 2, and things work fine. I hope
this makes things clearer.

Alan M. Carroll		                      carroll at s.cs.uiuc.edu
Grad Student / U of Ill - Urbana ...{pur-ee,convex}!uiucdcs!s!carroll
"Too many fools who don't think twice, too many ways to pay the price" - AP&EW



More information about the Comp.lang.c mailing list