第一维差分方法的访问冲突写入位置c ++

时间:2016-11-11 07:14:50

标签: c++

由于误差,我有一个问题需要处理第一维平流方程。 我获得了访问viloation写作位置'在我的控制台屏幕上。 这听起来像错误与内存分配有关,但我知道我有足够的内存空间来分配所有贵重物品。我不知道如何处理这件事。你能帮我一把吗?

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

class differencemethod {
private:
    unsigned short const velocity;
    unsigned short const tmax;
    float const dt;
    unsigned short const xmax;
    unsigned short const dx;

public:
    differencemethod(unsigned short tmpvelocity, unsigned short tmptmax, float tmpdt, unsigned short tmpdx, unsigned short tmpxmax)
    : velocity(tmpvelocity), tmax(tmptmax), dt(tmpdt), dx(tmpdx), xmax(tmpxmax) {
        float* x = new float[xmax + 1];
        unsigned short* xn = new unsigned short;
        for (unsigned short xn = 1; xn <= xmax; xn++) {
            x[xn] = 0.5*dx + (xn - 1)*dx;
        }
        double* f = new double[xmax + 1];
        for (unsigned short xn = 1; xn <= xmax; xn++) {
            f[xn] = exp(-(((x[xn] - 50)*(x[xn] - 50)) / (15 * 15)));;
        }
        delete[] x;
        x = NULL;
        cout << "initalization completed!\n";
        ofstream out;
        out.open("1strun.txt");
        out.precision(10);
        double* fnew = new double[xmax + 1];
        for (unsigned short t = 1; t <= tmax / dt; t++) {
            f[1] = 0;
            fnew[1] = 0;
            for (unsigned short xn = 2; xn <= xn; xn++) {
                fnew[xn] = f[xn] - ((velocity * dt) / dx)*(f[xn] - f[xn - 1]);
                f[xn] = fnew[xn];
                if (t == 100 || t == 300|| t == 500|| t == 700) {
                    if (xn == 2) {
                        out << "======" << t << "=====\n";
                        out << "1" << '\t' << "0\n";
                    }
                    out << xn << '\t' << fnew[xn] << endl;
                }
            }
            cout << t << " / " << (tmax / dt) << " completed\n";
        }
        out.close();
        delete[] f;
        f = NULL;
        delete[] fnew;
        fnew = NULL;
    }
};

void main(){
    differencemethod *firstq = new differencemethod(1, 700, 0.1, 1, 1000);
}

0 个答案:

没有答案