Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

results are not being written

25 views
Skip to first unread message

the...@hotmail.com

unread,
Feb 7, 2017, 10:59:55 PM2/7/17
to
Hi, I'm having problems with the following code:

#include <iostream>
#include <fstream>
#include "params.h"

extern void timestep(std::ofstream& fout, double* rho_prev, double* rho, double* rho_next, double* x);

int main() {
// Open output file
std::ofstream fout(outfilename);

// Define and allocate arrays
double* rho_prev = new double[npnts]; // time step t-1
double* rho = new double[npnts]; // time step
double* rho_next = new double[npnts]; // time step t+1
double* x = new double[npnts]; // x values

// Initialize
for (int i = 0; i < npnts; i++) {
x[i] = x1 + ((i-1)*(x2-x1))/ngrid;
rho_prev[i] = 0.0;
rho[i] = 0.0;
rho_next[i] = 0.0;
}

timestep(fout, &rho_prev[0], &rho[0], &rho_next[0], &x[0]);

// Close file
fout.close();
std::cout << "Results written to " << outfilename << std::endl;
}

So in this code, I have the function timestep that is defined in another .cc code. I know there's nothing wrong with this function because other executables that I construct with timestep in them run exactly as expected. Secondly, params.h contains externs for a .cc file called params.cc. params.cc contains a whole bunch of variables and their assignments. It also contains the following that is relevant to the above:

std::string outfilename = "result.txt"; // name of the file with the output data

I am basically making a list of x values that are sent to result.txt. I am also making time values that should be all 0. This is part of a sanity check I am performing for a large number of modules that I later link using a Makefile. The problem here is that when I run my Makefile, I get the output "Results written to result.txt" however when I look inside result.txt its empty. What is wrong with my code?

Fred.Zwarts

unread,
Feb 8, 2017, 4:03:01 AM2/8/17
to
schreef in bericht
news:f0cab520-a84e-49fc...@googlegroups.com...
1) Why don't you check the status of fout after opening the file?

2) Do you check the status of fout after output operations?

3) Are you sure that the function timestep does not have undefined
behaviour? Undefined behaviour includes expected behaviour, so when it has
expected results in other executables, it does not prove that it is correct.

4) The problem is probably in a part of the program that you do not show.
Reduce your program until it is small enough to present to this group and
still shows your problem. When we can reproduce the problem, then we may be
able to help you. Probably, you will find the cause of your problem already
yourself in the process.

Alf P. Steinbach

unread,
Feb 8, 2017, 4:05:20 AM2/8/17
to
That's probably where the problem you're concerned about, is.


> I know there's nothing wrong with this function

That's silly.

You know that's likely where the problem is.


[snip]


Cheers!, & do think about earlier advice to post a REPRODUCIBLE example,

- Alf

Juha Nieminen

unread,
Feb 8, 2017, 4:30:39 AM2/8/17
to
the...@hotmail.com wrote:
> double* rho_prev = new double[npnts]; // time step t-1
> double* rho = new double[npnts]; // time step
> double* rho_next = new double[npnts]; // time step t+1
> double* x = new double[npnts]; // x values

Why do you insist in doing this?

If you really need to allocate arrays dynamically, what exactly is wrong
with using std::vector for that?

There are good reasons why C++ offers dynamic allocation of arrays using
the 'new' keyword, but those reasons don't come up in actual practical
code very often. In 99% of cases, in practical code, std::vector is the
better choice (it does the exact same thing, but more safely because
it's a lot harder to leak memory with it).

the...@hotmail.com

unread,
Feb 8, 2017, 11:03:56 AM2/8/17
to
Ok well I figured out what the problem is. There's nothing wrong with the timestep function as I suspected, because other modules depend on this timestep function and they also produce textfiles that I have to compare to known values and I'm getting the correct results.

The problem is, my params.h is not being read. Because when I manually enter the parameters in the above code, the problem is solved and I get exactly the textfile that I want with the results I'm looking for.

Along with some macros, here is what I'm entering into my Makefile:

# The executable for the unit test for the evolution module
unittest: testtimestep.o params.o loops.o
$(CC) -o unittest testtimestep.o params.o loops.o

# Create the object files
params.o: params.cc
$(CC) $(CFLAGS) -o params.o params.cc

loops.o: loops.cc params.h
$(CC) $(CFLAGS) -o loops.o loops.cc

testtimestep.o: testtimestep.cc params.h
$(CC) $(CFLAGS) -o testtimestep.o testtimestep.cc

My apologies about the reproducible example thing. I've only started coding in c++ a month ago and besides writing some easy loops codes in Matlab I have no coding experience. This assignment I'm working on is made up of several very huge .cc files and it would be very overwhelming to post them all and explain what each one is doing.

The params.h contains the following:

// Preprocessor guards
#ifndef PARAMS_H
#define PARAMS_H
#include <string>

// List of all parameters and functions used by main.cc and loops.cc whose
// assignments are stored in params.cc
extern double c;
extern double tau;
extern double x1;
extern double x2;

extern double runtime;
extern double dx;

extern double outtime;
extern std::string outfilename;

extern void readParams(char* inputname);
extern void derParams();

extern int ngrid;
extern int npnts;
extern double dt;
extern int nsteps;
extern int nper;

#endif

And the externs are defined in another file, params.cc, which contains all the assignments of these variables, and some functions like readParams and derParams (that are not being used by the problem code). There is also nothing wrong with params.cc I think because other codes depend on params.cc and I'm getting all of the correct text results.
0 new messages