Error using blitz

The error is - CRT detected that the application wrote to memory after end of heap buffer. I am using visual studio 2019 and imported blitz++ by nuget

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
64
65
66
  #include <blitz/array.h>
#include <iostream>
#include <cstdlib>

using namespace blitz;
using namespace std;

/* This will evaluate the slopes. say if dy/dx = y, rhs_eval will return y.  */
const double sig = 10; const double rho = 28; const double bet = 8.0/3;
void lorenz(double x, Array<double, 1> & y, Array<double, 1> & dydx)
{
    /* y vector = x,y,z in components */
    dydx[0] = sig * (y[1] - y[0]);
    dydx[1] = rho * y[0] - y[1] - y[0] * y[2];
    dydx[2] = y[0] * y[1] - bet * y[2];
}

void rk4_fixed(double& x, Array<double, 1> & y, void (*rhs_eval)(double, Array<double, 1>&, Array<double, 1>&), double h)
{
    // Array y assumed to be of extent n, where n is no. of coupled equations
    int n = y.extent(0);

    // Declare local arrays
    Array<double, 1> k1(n), k2(n), k3(n), k4(n), f(n), dydx(n);

    // Zeroth intermediate step
    rhs_eval (x, y, dydx);
    k1 = h * dydx; f=y+0.5*k1;
    

    // First intermediate step
    rhs_eval(x + 0.5*h, f, dydx);
    k2 = h * dydx; f =  y+0.5*k2;

    // Second intermediate step
    rhs_eval (x + 0.5*h, f, dydx);
    k3 = h * dydx; f=y+k3;
 
    // Third intermediate step
    rhs_eval (x + h, f, dydx);
    k4 = h * dydx;
 
    // Actual step
    y += k1 / 6. + k2 / 3. + k3 / 3. + k4 / 6.;
    x += h;
    
    return; //# goes back to function. evaluate y at x+h without returning anything
}

int main()
{
    Array<double, 1> y(3);
    y = 1,1,1;
    cout << y << endl;
    double x=0, h = 0.05;
    while(x<20) {
        rk4_fixed(x,y,lorenz,h);
        cout << x;
        for(int k =0; k<3; k++) {
            cout << ", "<< y(k);
        } 
        cout << endl;
    } 
    return 0;
}
Last edited on
L10. Array<double, 1> & y. This means that there is only 1 element addressed as y[0]. If you want 3 elements y[0], y[1], y[2] then Array<double, 3>& y. Same for dydx etc. Similar for L18. Also L52. This should be :

 
Array<double, 3> {1, 1, 1};


L53 - doesn't do what you think it does. It sets y to 1 - which should generate an error. This is the comma operator syntax where the operands are evaluated left to right between each , with the final value the one assigned. Remove.

Last edited on
L10. Array<double, 1> & y. This means that there is only 1 element addressed as y[0].

No, it means it has only one dimension.

L52 creates a one-dimensional array of size 3.


L53 - doesn't do what you think it does. It sets y to 1 - which should generate an error.

Blitz++ overloads the comma operator so this should work.
Sorry. I was think thinking that Array was referring to std::array - not blitz Array! This is the confusion you get when namespaces are explicitly specified and using namespace is used! I also misread Array as array.
Topic archived. No new replies allowed.