Is &a[NTHINGS] legal

William E. Davidsen Jr davidsen at steinmetz.ge.com
Thu May 5 03:34:32 AEST 1988


I don't see that there should be any bounds checking until the pointer
or address is dereferenced. Doing a check is of dubious use and will
probably break as many valid programs as it helps.

Consider:
  char *x, a[NVAL];

  /* if this is legal */
  x = &a[0];
  /* and this is legal */
  x += NVAL;
  /* then why try to make this illegal? */
  x = &a[NVAL];

There are (valid) reasons for wanting to take an address outside the
range of an array. Consider an algorithm in which negative subscripts
are heavily used (say from Pascal, PL/I, Algol-60, etc). You can do an
add each time to normalize the values of the subscript, but it is (a)
slower, and (b) hard to read.

Therefore:
  char *x, a[NVAL];
  x = &a[NVAL+49];		/* for use with negative subscripts */
  x[-50] = 'a';			/* same as a[NVAL] */

If dpANS were going to make C into Pascal, it should have been done by
having real enums instead of the botch we have now. The feature appeared
(I was told) when cpp ran out of symbol space. It was kept as is to "not
break existing programs."
-- 
	bill davidsen		(wedu at ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.lang.c mailing list