jcaiuwyo / cantera

Automatically exported from code.google.com/p/cantera
0 stars 0 forks source link

Fail on Compile due to issue in MolalityVPSSTP.cpp #26

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Attempting to compile the latest version of Cantera 1.8 source code

What is the expected output? What do you see instead?
Expected output is an executable I can use to install Cantera.  Instead Visual 
Studio 2008 produces 35 errors, all of which stem from lines 944 - 955 in 
MolalityVPSSTP.cpp.  The error suggests the variables on these lines should be 
assigned values, but they are not.  Example:

error C2057: expected constant expression

What version of the product are you using? On what operating system?
The source code available on 8 October 2010, on WinXP SP3, with Visual Studio 
2008 and MATLAB 2008a.

Please provide any additional information below.

Original issue reported on code.google.com by richard....@gmail.com on 8 Oct 2010 at 8:10

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
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:

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
Fixed in r875.

Original comment by yarmond on 14 Dec 2011 at 5:57