Access violation writing location c++ for 1st dimensional difference method -
i have problem deal 1st dimensional advection equation becauseof error. got 'access viloation writing location' in console screen. sounds error memory allocation know have enough memory space allocate valuables. don`t know how deal this. give me hand please?
#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; (unsigned short xn = 1; xn <= xmax; xn++) { x[xn] = 0.5*dx + (xn - 1)*dx; } double* f = new double[xmax + 1]; (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]; (unsigned short t = 1; t <= tmax / dt; t++) { f[1] = 0; fnew[1] = 0; (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); }
Comments
Post a Comment