Greeting Everyone
I’m trying to compile and run a multi-language code in C, C++ and fortran using gcc, g++ & f77 respectively in UNIX. My program consists of two parts, one in C, the other in C++. They interface via a main() writen in C+, while the fortran code can be ignored for this case.
I have been having numerous issues with this, most noticabbly a Segmentation Error thats been occuring as I run the executable. The previous two topics of mine have whittled it down, unfortunatly nothing seems to be solving the problem outright other than completely removing any input/output processes in either half of my program, which just isn’t feasible.
Accessing public class memory from C++ using C Output conflicts between C & C++
I need to locate the reason why I recieve Segmentation Errors when I use input/outputs in both parts of my program. All sources compile, all link successfully and I know that each part (C & C++) works when linked alone with no such Segmentation Errors (with modifications to let them work alone of course). I’ve included all the code that interfaces between the two parts and performs input/output functions.
Any help would be much appriciated.
Makefile
products: SlowDynamic.exe SlowDynamic.exe: main.o SA.o mersenne.o CFE.o BCs.o EMatrix.o Numbering.o KMatrix.o Solve.o MA_57.o blas.o MA_57_Depend.o Metis.o f77 -L/usr/sfw/lib -R/usr/sfw/lib -lgcc_s -lstdc++ -o SlowDynamic.exe main.o \ SA.o mersenne.o CFE.o MA_57.o blas.o MA_57_Depend.o Metis.o\ BCs.o EMatrix.o Numbering.o KMatrix.o Solve.o main.o: main.cpp g++ -c -o main.o main.cpp SA.o: SA.cpp g++ -c -o SA.o SA.cpp mersenne.o: mersenne.cpp g++ -c -o mersenne.o mersenne.cpp CFE.o: CFE.c gcc -c -o CFE.o CFE.c MA_57.o: MA_57.f f77 -c -o MA_57.o MA_57.f blas.o: blas.f f77 -c -o blas.o blas.f MA_57_Depend.o: MA_57_Depend.f f77 -c -o MA_57_Depend.o MA_57_Depend.f Metis.o: Metis.f f77 -c -o Metis.o Metis.f BCs.o: BCs.c gcc -c -o BCs.o BCs.c EMatrix.o: EMatrix.c gcc -c -o EMatrix.o EMatrix.c Numbering.o: Numbering.c gcc -c -o Numbering.o Numbering.c KMatrix.o: KMatrix.c gcc -c -o KMatrix.o KMatrix.c Solve.o : Solve.c gcc -c -o Solve.o Solve.c clean: rm *.o Main.exe *.gpi
main.ccp
#include <iostream> #include 'SA.h' using namespace std; int main() { Initial.Initialize(); Parent.SA(Initial.Write); system ('PAUSE'); return 0; }
SA.h
#ifndef SA_H #define SA_H #include <vector> class SimAnneal { std::vector< std::vector<float> > DensityDomain; float Solid_Ele_Num, Void_Ele_Num; float Solid, Void; double Energy; double Time; void Metropolis (double, int, int); void Next_State (double, int); double Schedule (double, int); double ObjFunction (); void Distribute (); void Mutate (); void Convert (); void PrintDomain (); void WriteResults (double, double, double, double, double); public: int x_max, y_max; ... std::vector<float> DensityArray; std::vector<float> EnergyArray; ... void SA (int); void Initialize (); }; extern SimAnneal Initial, Parent, Child, Mutation, Best; #endif
SA.cpp
include <math.h> #include <iostream> #include <fstream> #include <time.h> #include <vector> #include 'SA.h' #include 'CFE.h' #include 'randomc.h' using namespace std; SimAnneal Initial, Parent, Child, Mutation, Best; ... void SimAnneal::Initialize () { x_max = ReturnX(); y_max = ReturnY(); EnergyArray.resize(x_max*y_max); DensityArray.resize(x_max*y_max); ... Energy = ObjFunction(); } ... void SimAnneal::PrintDomain () { static ofstream OutputFile; if (!OutputFile.is_open()) { char FileName [] = 'DensityDomain.txt'; OutputFile.open(FileName); if (!OutputFile) { cerr << 'Failed to open ' << FileName << endl; exit(EXIT_FAILURE); } //cout << '\nGenerating 'DensityDomain.txt'... \n' << endl; } for (int y = 0; y < y_max; y++) { for (int x = 0; x < x_max; x++) { OutputFile << DensityDomain[y][x] << ' '; } OutputFile << endl; } OutputFile.close(); } void SimAnneal::WriteResults (double i, double T, double x, double y, double z) { static ofstream OutputFile; if (!OutputFile.is_open()) //check is file has been opened { char FileName [] = 'Results.txt'; OutputFile.open(FileName); if (!OutputFile) { cerr << 'Failed to open ' << FileName << endl; exit(EXIT_FAILURE); //abort program } //cout << '\nWriting to file in progress... \n' << endl; OutputFile << 'Iterations' << '\t' << 'Temperatures' << '\t' << 'Sum Strain Energy' << endl; //<< 'SwapNum1' << '\t' << 'SwapNum2' << '\t' << 'Distance' << endl; OutputFile << endl; Initial.Time = (int)time(0); } OutputFile << i << '\t' << T << '\t' << z << endl; //'\t' << y << '\t' << z << endl; if (i == N_max || T <= T_abs) { Parent.Time = (int)time(0); OutputFile << endl << 'Settings: ' << endl << 'Initial Temperature: ' << Initial.Temp << endl << 'Temperature Iterations: ' << i << endl << 'Step Iterations: ' << N_step << endl << endl << 'Results: ' << endl << 'Final Temperature: ' << Temp << endl << 'Minimum: ' << Energy << endl << 'Computational Time (s): ' << (Parent.Time-Initial.Time) << endl; OutputFile.close(); } }
CFE.h
#ifdef __cplusplus extern 'C' { #endif int ReturnX (); int ReturnY (); void CFE(float density[], float energy[], int Length); #ifdef __cplusplus } #endif
CFE.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include 'BCs.h' #include 'EMatrix.h' #include 'Numbering.h' #include 'KMatrix.h' #include 'fg_types.h' #include 'Solve.h' int ReturnX () { FILE *infile; infile = fopen('test05', 'r'); int elemX,elemY; fscanf(infile, '%i %i', &elemX, &elemY); fclose(infile); return elemX; } int ReturnY () { Same but returns elemY } void CFE(float density[], float energy[]) { // Extensive use of fscanf(), printf() & fprintf() // and the following: FILE *outfile; outfile = fopen('File.txt', 'w'); if(outfile == NULL){ } else{ for(n=0;n<8;n++) { for(m=0;m<8;m++) { fprintf(outfile,'%f',KE[n][m]); fprintf(outfile,'\t'); } fprintf(outfile,'\n'); } } fclose(outfile); }
I’d suggest going through your code with a critical eye, and checking out everything that looks even remotely odd.
I’d do it for you but C++ isn’t in my rotation at the moment and I’m tripping on false positives. For example, this caught my eye:
Half way down you’re testing if
OutputFileis null, after already callingis_open()andopen()on it. It looked to me as if either 1)OutputFilewon’t be null or 2) you shouldn’t be calling methods on it before you test it. But I was wrong.See what I mean?