Re: C Fronts (Henry Spencer)
Fri, 9 Aug 1991 18:14:01 GMT

          From comp.compilers

Related articles
C Fronts (1991-08-04)
Re: C Fronts (1991-08-05)
Re: C Fronts (1991-08-07)
Re: C Fronts (Chris Dollin) (1991-08-08)
Re: C Fronts (1991-08-09)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (Henry Spencer)
Keywords: C, optimize
Organization: U of Toronto Zoology
References: 91-08-032
Date: Fri, 9 Aug 1991 18:14:01 GMT

In article 91-08-032 Chris Dollin <> writes:
> is *not* legal to index an
>array outside its bounds in C. For example, ``a[10][-1]'' is illegal, even
>though blindly performing the address calculation would get a reference
>inside the array.

This is not quite correct. C array indexing is defined in terms of adding
integers to pointers. (a[b] is *(a + b) by definition.) The results of
taking a pointer outside the array by this means are undefined -- not
"illegal", but "undefined", meaning that what happens is entirely the
compiler's choice. (There is one obscure exception, which I omit for
clarity.) Relying on undefined behavior is unwise, and the compiler is
technically allowed to reject such a program, but it is not required to
do so. In the example, it may quite legitimately generate code that does
indeed give you a reference within the array, without any complications.
Such a compiler is not defective.
Henry Spencer @ U of Toronto Zoology, utzoo!henry
[Norman Diamond <> also pointed this out. Clearly
I should put my copy of the standard back in the bathroom so I read it again.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.