Windows has triggered a breakpoint in xxx.exe.

Hello, there. I build my codes well, while when I run the .exe.
A message box coms out:
Windows has triggered a breakpoint in spline.exe.
This may be due to a corruption of the heap, which indicates a bug in spline.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while spline.exe has focus.
The output window may have more diagnostic information.

Does anyone know how to fix it? I know it could be out of memory,but how should I change? Thank you
Here is the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
//  spline.h
//

#ifndef _spline_h
#define _spline_h

#include "usrdef.h"

void spline(double x[], double y[], const int n, double yp1, double ypn, double y2[])
{
    int i, k;
    double p, qn, sig, un;//, *u

    vector<double> u(n-1);
    if (yp1 > 0.99e30)
        y2[1] = u[1] = 0.0;
    else{
        y2[1] = -0.5;
        u[1] = (3.0/(x[2]-x[1]))*((y[2]-y[1])/(x[2]-x[1])-yp1);
    }
    for (i=2; i<=n-1; i++) {
        sig = (x[i]-x[i-1])/(x[i+1]-x[i-1]);
        p = sig*y2[i-1]+2.0;
        y2[i] = (sig-1.0)/p;
        u[i] = (y[i+1]-y[i])/(x[i+1]-x[i])-(y[i]-y[i-1])/(x[i]-x[i-1]);
        u[i] = (6.0*u[i]/(x[i+1]-x[i-1])-sig*u[i-1])/p;
    }
    if (ypn > 0.99e30)
        qn = un =0.0;
    else{
        qn = 0.5;
        un = (3.0/(x[n]-x[n-1]))*(ypn-(y[n]-y[n-1])/(x[n]-x[n-1]));
    }
    y2[n] = (un-qn*u[n-1])/(qn*y2[n-1]+1.0);
    for (k=n-1; k>=1; k--)
        y2[k] = y2[k]*y2[k+1]+u[k];
    u.clear();
    //delete[] u;
}

void splint(double xa[], double ya[], double y2a[], int n, double x, double yy)
{
    void nrerror(char error_text[]);
    int klo, khi, k;
    double h, b, a;
    
    klo = 1;
    khi = n;
    while (khi-klo > 1) {
        k = (khi+klo) >>1;
        if (xa[k] >x) khi=k;
        else klo = k;
    }
    h = xa[khi]-xa[klo];
    if (h == 0.0) cout<<"Bad Xa input to routine splint"<<"\n";
    a = (xa[khi]-x)/h;
    b = (x-xa[klo])/h;
    yy = a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]+(b*b*b-b)*y2a[khi])*(h*h)/6.0;
}


#endif 
I'd guess that you have an out of bounds problem. Note that the index of an array starts with 0. The last valid index is the number of array elements minus one.
Thank you coder777, considering your advices
I modified y2[1],u[1] to y2[0],u[0] in the beginning, and (i=2;i<=n-1;i++) to (i=1;i<n-1), (k=n-1;k>=1,k--) to (k=n-2;k>=0;k--). And now it comes out with another message:
A buffer overrun has occurred in xxx.exe which has corrupted the program's internal state.
Do you know how does it come from? Thanks
There are a lot of places where you use n directly as an index (like line 33/35/49) which is out of bounds.

Instead of decreasing the index (rather inconvenient) you might increase the size of the array and leave your current calculation like they are. But still check whether you have indexes out of bounds.
Yes, coder777. I lost them 'n', and now I change all of it.
When I compile and run my code, I briefly see the command prompt appear on the screen before disappearing and see the following in the debugger
'The program '[5116] spline.exe: Native' has exited with code 0 (0x0).'
no idea about this error, and I can not see the result. Thank you
I put a getchar(); into the main, and now it doesn't disappear.
The result is also coming out correctly.
Thank you very much, coder777! thumbs up
Topic archived. No new replies allowed.