On Thu, Sep 19, 2024 at 7:52 PM Rich Salz <rich.salz(a)gmail.com> wrote:
In my first C programming job I saw the source to V7 grep which had a
"foo[-2]" construct.
That sort of thing is very dangerous with modern compilers. Does K&R C
require that variables be allocated in the order that they are declared?
If not, you're playing with fire. To get decent performance out of modern
processors, the compiler must perform data placement to maximize cache
efficiency, and that practically guarantees that you can't rely on
out-of-bounds array references.
Unless "foo" were a pointer that the programmer explicitly pointed to the
inside of a larger data structure. In that case you could guarantee that
the construct would work reliably. But by pointing into the middle of
another data structure you've created a data aliasing situation, and that
complicates compiler data flow analysis and can block important
optimizations.
Things were much simpler when V7 was written.
-Paul W.