Greetings Everyone
I am currently trying to write a multi-language program (C, C++ and fortran) though am achieving segmentation errors. I've ruled out vectors and the like in: http://stackoverflow.com/questions/666320/accessing-public-class-memory-from-c-using-c
I've narrowed now the cause to the use of 'cout' experssions in my C++ segments and printf(...) in C segments. Depending on which order I run these at I always get segmentation error when using the 2nd type, like so:
- cout first, then printf(...) will crash at first C output function
- printf(...), then cout will crash at first C++ output function
I am #include <iostream>
in my C++ sources, and #include <stdio.h>
& #include <stdlib.h>
in my C sources.
Is there a library conflict that I am not aware of?
Requested code:
main.cpp
#include <iostream>
#include <vector>
#include "CFE.h"
ios::sync_with_stdio(true);
using namespace std;
vector<float> DensityArray;
vector<float> EnergyArray;
int main()
{
int X = ReturnX ();
int Y = ReturnY ();
cout << "X " << X << endl;
cout << "Y " << Y << endl;
EnergyArray.resize(Y*X);
DensityArray.resize(Y*X);
CFE(&DensityArray[0], &EnergyArray[0]);
cout << "X " << X << endl; //causes Segmentation break
cout << "Y " << Y << endl; //causes Segmentation break
system ("PAUSE");
return 0;
}
CFE.h
#ifdef __cplusplus
extern "C" {
#endif
int ReturnX ();
int ReturnY ();
void CFE(float density[], float energy[]);
#ifdef __cplusplus
}
#endif
CFE.cpp
#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 ()
{
FILE *infile;
infile = fopen("test05", "r");
int elemX,elemY;
fscanf(infile, "%i %i", &elemX, &elemY);
fclose(infile);
return elemY;
}
void CFE(float density[], float energy[])
{
FILE *infile;
infile = fopen("test05", "r");
int elemY, elemX;
fscanf(infile, "%i %i", &elemX, &elemY); //Will cause Segmentation break
int n;
float * dens;
dens = density;
float * engy;
engy = energy;
int Length = 10;
for ( n = 0; n < Length; n++)
{
engy[n] = n;
}
}