tags:

views:

258

answers:

8

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:

  1. cout first, then printf(...) will crash at first C output function
  2. 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;
    }
}
+2  A: 

Any particular reason why you don't just use printf in your C++ code? Sure, it's not like what all the cool kids do, but it should still work.

Paul Tomblin
printf works. Worst comes to worst can replace cout with printf in my C++ code. Still an odd occurance though.
Raugnar
A: 

what is being output? does it crash from segment errors if you do printf("hello world?");

partouf
+1  A: 

What platform is this on? If you have ios::sync_with_stdio(bool) available on your platform, call it with

ios::sync_with_stdio(true)

at the beginning of your program (call it from C++). Does this help?

antti.huima
UNIX, unfortunatly doesn't look like it has it as it wont compile with that
Raugnar
It should default to true in any case. (and it's not platform-dependent, but part of the C++ standard. And of course, the full name is std::ios::sync_with_stdio.
jalf
Calling it above my main.cpp says std::ios undeffined.
Raugnar
A: 

The quick answer here is no, it absolutely 100% should not crash just because you are mixing printf and cout. The only difficulty with mixing the two is that the respective output buffers will not be synced by default, so the order of the output may be a bit mixed up. But it definitely should not crash.

At a quick guess, perhaps you have a formatting problem with printf -- you're passing a double when it expects a short int, or something like that. That will lead to unpredictable problems, which might well shift depending on the order of function calls in your code.

Sol
A: 

ReturnY() is returning X instead of Y

(ps. close your files)

partouf
Good point with the files. Fixed but still having same issue.
Raugnar
+2  A: 

You need to check that the files are opened correctly - i.e. the the pointer returned by fopen() is not NULL. Also,

int ReturnY ()
{
    FILE *infile;
    infile = fopen("test05", "r");

    int elemX,elemY;
    fscanf(infile, "%i %i", &elemX, &elemY);
    return elemX;
}

I take it return elemx should be return elemy?

anon
Corrent, its edited to state the correct
Raugnar
Again, thank you for the help, seems to be the incorrect reading being taken from the file and thus the wrong values
Raugnar
@Raugnar - was the problem that infile was NULL or that the crash happened later because ReturnY() returned something nonsensical? If the latter (actually in either case), running under a debugger would have given you better information than your apparent divide and conquer debugging approach.
Michael Burr
+1  A: 

When I mix cout and printf in an application, I find it useful to have all my C++ code do the equivalent of:

fflush(stdout);
cout << ...whatever... << flush;

This way stdout and cout never both have unflushed buffers at any point in time.

Extra care should be taken for apps where multiple threads access stdout and/or cout.

Mr Fooz
Or you can tie the streams and not worry about it.
Martin York
I assume you mean via "ios::sync_with_stdio", or is there some other portable way?
Mr Fooz
A: 

I don't see a printf(), so I can't comment on it. You should show what printf()s you're using, providing a complete sample program that fails as you describe.

As far as the comment about a crash on fscanf(), as Neil Butterworth says, you need to check the return value of fopen(). If, for some reason, the fopen() fails (which it doesn't look like it should), fscanf() might crash. (If it can't read integers, it should deal with it.) You're not checking anything in I/O. (Alternatively, the I/O system might be messed up from something previous.)

I'd suspect corruption somewhere in the I/O memory, but I don't see enough to conclude anything.

David Thornley