I am trying to call a Fortran subroutine in C++.
this is the subroutine start:
subroutine fireballess(ear,ne,parames,ifl,photar,photer)
integer ne,ifl
real*4 ear(0:ne),parames(10),photar(ne),photer(ne)
The subroutine works fine in Fortran, but when I try to call it in C++ I get a segmentation fault. Here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
extern "C" void fireballess_( double *fear, int fne,double* fparames, nt fifl, double *fphotar, double *fphoter);
int main(int argc, char ** argv)
{
int ne,ifl;
double *ear;
double *parames;
double *photar;
double *photer;
parames = new double[9];
parames[0]=4.3;
parames[1]=0.23;
parames[2]=0.5;
parames[3]=0.5;
parames[4]=1.5;
parames[5]=1.;
parames[6]=1000.;
parames[7]=2.15;
parames[8]=3.;
ne = 3;
ear = new double[ne];
ear[0] = 0.;
ear[1] = 20.;
ear[2] = 40.;
ifl=2;
photar = new double[ne];
photer = new double[ne];
// Check on variables initialization
for (int i=0;i<=2;i++) cout << ear[i] <<",";
cout <<" "<< ne<<" ";
for (int i=0;i<=8;i++) cout << parames[i] <<",";
cout <<" "<< ifl <<" "<< photar[0] <<" "<< photer[0] << endl;
cout << "Calling a Fortran subroutine" << endl;
cout << "===============================" << endl;
// call to the subroutine -->segmentation fault
fireballess_(ear,ne,parames,ifl,photar,photer);
for (int i=0;i<=ne;i++){
cout << "ear = " <<ear[i-1]<< " - "<<ear[i] << endl;
cout << "photar = " << photar[i] << endl;
cout << "photer = " << photer[i] << endl << endl;
}
delete[] ear;
delete[] parames;
delete[] photar;
delete[] photer;
}
The program crashes on the call to the subroutine. I am not very experienced in C++ or Fortran coding, so I am not sure about what to do. So far I have checked the format of the variables passed to the subroutine was correct, and it looks so.
Thanks in advance for any help
---------EDIT------ after reading some comments I revised the code as follows, still getting the same segmentation fault error when the routine is invoked:
using namespace std;
extern "C" void fireballess_( std::vector<float> fear, int fne,std::vector<float> fparames, int fifl, std::vector<float> fphotar, std::vector<float> fphoter);
int main(int argc, char ** argv)
{
int ne,ifl;
ifl=2;
ne = 3;
std::vector<float> parames = {4.3,0.23,0.5,0.5,1.5,1.,1000.,2.15,3.};
std::vector<float> ear={0,20,40};
std::vector<float> photar;
std::vector<float> photer;
cout << "Calling a Fortran subroutine" << endl;
cout << "===============================" << endl;
fireballess_(ear,ne,parames,ifl,photar,photer);
for (int i=0;i<ne;i++){
cout << "ear = " <<ear[i-1]<< " - "<<ear[i] << endl;
cout << "photar = " << photar[i] << endl;
cout << "photer = " << photer[i] << endl << endl;
}
}
real*4is a four-byte floating point type, and the C++doubletype is an eight byte floating point type. The C++ typefloatis a single-precision four-byte type.photar = new double[ne];--for (int i=0;i<=ne;i++){--cout << "photar = " << photar[i] << endl;-- What happens wheniis equal tone? You have an off by one error and an invalid access. A C++forloop is always suspicious when<=is used as the loop condition, and the variable is used as an array index. Also, you should drop usingnew[]and start to usestd::vector.int(theifl) to your routine. Have you verified thatsizeof(int)is exactly the byte size of the integers used by your FORTRAN program? If not the same byte size, you are corrupting the program stack.