I am attempting to implement the Cublic Spline Extrema Algorithm published in 1996.
I am not an accomplished programmer, and it is clear the author is, and is an order of magnitude or two smarter than I. However when I try and compile this, it seg faults on a simple out-of-bounds array access. I find it hard to believe that this algorithm was published with what seems, to me, to be a trivial mistake. Thus, I conclude the misunderstanding is likely to be mine and not the authors. (e.g perhaps he was using a trick I am unaware of that may have worked in c 20 years ago...).
The first segfault happens here, with an attempt to access diag[-1]
:
float *diag; /* ptr to matrix diagonal array */
for (i = 0; i < num_pnts - 1; i++){
diag[i-1] = x[i+1] - x[i];
assert(diag[i-1] > 0);}
And, if I fix the above for loop to start at1
, then another segfault will occur at the next loop, for a similir reason, (diag[i-2]-->diag[-1]
)
for (i = 1; i < num_pnts - 1; i++)
right[i-1] = 6.0 * ((y[i+1]-y[i])/diag[i-1]-(y[i]-y[i-1])/diag[i-2]);
A link to the full source code is here
So my question is, this algorithm is published by a very accomplished individual, not a first year CS student. So while these look like errors to me (and indeed, segfault on a 2019 system), is there something else going on here I am missing? e.g would this OOB error have had a different result with a 1996 c compiler or something?
Question: are these actually mistakes?