Closed GoogleCodeExporter closed 9 years ago
It looks like I misunderstood the error... the problem is with the array
declarations on lines 944-955 in MolalityVPSSTP.cpp:
int kk = nSpecies();
double x[kk];
double molal[kk];
double mu[kk];
double muss[kk];
double aMolal[kk];
double acMolal[kk];
double hbar[kk];
double sbar[kk];
double ubar[kk];
double cpbar[kk];
double vbar[kk];
When I replace [kk] in each array declaration with an integer (I happened to
choose [6]) it compiles. To see if maybe nSpecies() wasn't providing a
legitimate value for kk, I left everything along and tried setting kk = 6
instead of nSpecies(), but even that won't compile. It seems like there is a
problem with kk... perhaps its type is wrong?
The three errors for the first array declaration are:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'x' : unknown size
It has been a long time since I've looked at C++ code, so my efforts to debug
this are going very slowly. Any help would be appreciated!
Thanks,
Rick Stroman
Original comment by richard....@gmail.com
on 14 Oct 2010 at 5:58
[deleted comment]
Hi,
the compiler needs to know how big the arrays are so the program can allocate
enough memory on the stack. Unfortunately the size is unknown until the program
is running. So try allocate memory dynamically:
int kk = nSpecies();
double *x = new double[kk];
double *molal = new double[kk];
double *mu = new double[kk];
double *muss = new double[kk];
double *aMolal = new double[kk];
double *acMolal = new double[kk];
double *hbar = new double[kk];
double *sbar = new double[kk];
double *ubar = new double[kk];
double *cpbar = new double[kk];
double *vbar = new double[kk];
This way, the program does not need to know the array sizes during compiling.
However, you have to delete the arrays at the end of the function with:
delete [] x;
delete [] molal;
delete [] mu;
delete [] muss;
delete [] aMolal;
delete [] acMolal;
delete [] hbar;
delete [] sbar;
delete [] ubar;
delete [] cpbar;
delete [] vbar;
I don't really know where to insert it properly because I never used
try/catch-blocks, so I inserted it at the end of the try-block. Please correct
me if I'm wrong.
I can't remember that I had this problem with Linux. Maybe gcc is less strict?!
Best wishes,
Alex
Original comment by Alexande...@gmail.com
on 26 Oct 2010 at 8:01
Attachments:
Variable-length arrays are part of the C99 standard, but not C++. The best
solution here is to use std::vector, e.g.
std::vector<double> x(kk);
This is preferable to the new/delete option because the memory will be
deallocated even if an exception is raised in the function.
Original comment by yarmond
on 27 May 2011 at 10:00
Fixed in r875.
Original comment by yarmond
on 14 Dec 2011 at 5:57
Original issue reported on code.google.com by
richard....@gmail.com
on 8 Oct 2010 at 8:10