I call the Fortran 77 function from C ++, which passes the file descriptor, line and length. The files open successfully and the Fortran routine exits. However, in C ++ code, the line that was passed to fortran is corrupted. When the bottom of the openFile function is reached, the program crashes.
Failure only appears in release, but not in debugging. Marking the lines, I see that in the release the variable fileNameToFortran is full of garbage.
thanks for the help
I use ifort with the following compiler flags in the release (Windows 7 machine (32 bit)): / names: lowercase / f77rtl / traceback / iface: cref / threads / recursive / LD
and debug: / names: lowercase / f77rtl / traceback / iface: cref / threads / recursive / LDd / Zi / debug: full / check: all / traceback
Here is the C code:
typedef void (FORTCALL *sn_openfile_func) (int *, char[], int *, int); void openFile(const int fileHandle, const std::string fileName) { int fileHandleToFortran = fileHandle; char fileNameToFortran[20]; assert(fileName.size() < 20); strcpy(fileNameToFortran, fileName.c_str()); int lstr = strlen(fileNameToFortran); openfile_func_handle(&fileHandleToFortran, fileNameToFortran, &lstr, lstr); }
Here is the Fortran code:
SUBROUTINE SN_OPENFILE(FILENR,FILENAME,FSIZE) !DEC$ ATTRIBUTES DLLEXPORT :: SN_OPENFILE IMPLICIT NONE INTEGER FILENR, FSIZE CHARACTER FILENAME*FSIZE OPEN (FILENR,FILE = FILENAME, & ACCESS = 'SEQUENTIAL' , STATUS = 'REPLACE', ERR=222) GOTO 333 222 WRITE(*,*) 'Error opening file' 333 END