[TUHS] Maximum Array Sizes in 16 bit C

Douglas McIlroy douglas.mcilroy at dartmouth.edu
Sat Sep 21 01:24:16 AEST 2024


Apropos of the virtue of negative subscripts.

> by pointing into the middle of another data structure you've created a
data aliasing situation

Not if all references are relative to the offset pointer. The following
example is silly,  but I recently used exactly this trick to simplify
calculations on a first-quadrant (x,y) grid by supplying "out-of-bounds"
zeroes at (x,-2), (x,-1) and (-1,y). It was much cleaner to access the
zeroes like ordinary elements than to provide special code to handle the
boundary conditions.

/* Fill an N-element array with Fibonacci numbers, f(i), where 0<=i<N.
   The recursion accesses a zero "out of bounds" at i=-1 */

const int N = 100;

int base[N+1];
#define fib(i) base[(i)+1]

void fill() {
    int i;
    fib(0) = 1;
    for(i=1; i<N; i++)
        fib(i) = fib(i-2) + fib(i-1);
}

Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.tuhs.org/pipermail/tuhs/attachments/20240920/31c89db6/attachment.htm>


More information about the TUHS mailing list