Hi all,

I believe there's a bug in the conjugate gradients algorithm. It occurs when the algorithm is "unable to satisfy the armijo condition" (ie. when the current line search is unable to sufficiently minimise the function). The error occurs in v3p\netlib\napack\cg.c when exiting function cg_. The error message in VS is: "Run-Time Check Failure #2 - Stack around the variable z__ was corrupted", which usually means z__ was accessed out of bounds. On line 120 of cg.c it is declared as z__[50], however line 1356 accesses z__[*j] where *j=2,3,...,53 at run-time. (N.B: In this subroutine the pointer z__ has been locally decremented so it is safe to access indices 1-50 instead of 0-49).

The following code reproduces the error. The function to be minimized is intentionally bad, but it should not crash the program:

class func : public vnl_cost_function
{
public:
    func() : vnl_cost_function(1) {}
    double f(vnl_vector<double> const& x)
    {
        return 0;
    }
    void gradf(vnl_vector<double> const& x, vnl_vector<double>& gradient)
    {
        gradient[0] = 1;
    }
};

int main()
{
    vnl_vector<double> guess(1, 0);

    func f;
    vnl_conjugate_gradient minimizer(f);
    minimizer.minimize(guess);
}


A band-aid solution is to change the declaration of z__[50] to z__[53], and similarly y[50] to y[53].

David.