On 07.02.2017 04:16,
the...@hotmail.com wrote:
> #include <iostream>
> #include <fstream>
> #include <cmath>
> #include "params.h"
>
> // Define and allocate arrays
> double* rho_prev = new double[npnts]; // time step t-1
> double* rho = new double[npnts]; // time step t
> double* rho_next = new double[npnts]; // time step t+1
> double* x = new double[npnts]; // x values
>
> // Open output file
> std::ofstream fout(outfilename);
>
> // Declare function for timesteps
> void timestep();
>
> // Function for main computation
> void myLoops() {
> //some stuff
>
> // Take timesteps
> timestep();
>
> // Close file
> fout.close();
> std::cout << "Results written to " << outfilename << std::endl;
>
> // Deallocate arrays
> delete[] rho_prev;
> delete[] rho;
> delete[] rho_next;
> delete[] x;
> }
>
> // Function to take timesteps
> void timestep() {
> //some stuff
> }
>
>
> So I am having a problem with this code.
Well, to get more specific help you need to post a reproducible example.
Some easily recognizable issues with the above:
• Global variables.
They allow distant parts of the code to affect each other, in ways
that one can't grok. Nothing can be relied on where these are involved.
In your case perhaps deallocation is performed multiple times but
allocation is performed just once, maybe, but even if that's the case
there can still be many hidden problems: just get rid of all globals.
• Raw `new` and `delete`.
It's not smart to try to reinvent the automobile, trying to get
everything right, including the precision-machined engine parts and the
very special alloys used there. Instead just buy, borrow or steal an
automobile that works. The `std::vector` one is not so sexy but it's
dependable, works all right and is essentially free.
Cheers & hth.,
- Alf