[R] fortran package crashes

Karl May may.ka38 at yahoo.de
Mon Jun 23 11:06:29 CEST 2014


Hi all,

I have written a small Fortran routine to be attached to R for private use, 
that is reading matrices written to binary files by a Fortran program (I could 
not get "readBin" to read it). Unfortunately, when using the package R crashes 
every now and then, but not always, with a segmentation fault. When restarting 
R, it very often happens that the same read command, parameterised as before, 
is successful. Since the routine compiles without errors when installing it 
into R, I have no idea where to search further for the bug and it would be 
great if you could have a look at the source code below.

Thank you very much.


readfortran <- function(filename,nrows,ncols){
    dat <- matrix(0,ncol=ncols,nrow=nrows)
    ISStat <- c()
    out <- .Fortran("readfortran",
                    csfilename=as.character(filename),
                    ISNCol=as.integer(ncols),
                    ISNRow=as.integer(nrows),
                    RMOut=dat,
                    ISStat=as.integer(ISStat),
                    Package="readfortran")
    return(out)
}

Subroutine readfortran(csfilename,ISNcol,ISNrow,RMOut,ISStat)
  Implicit None
  Integer, Parameter :: IkXL=Selected_Int_Kind(12)
  Integer, Parameter :: IkS=Selected_Int_Kind(2)
  Integer(IkS), Parameter :: RkDbl=Selected_Real_Kind(15,100)
  Character(len=100), intent(inout) :: CSFileName
  Integer(IKXL), intent(inout) ::ISNCol, ISNrow
  Real(rkdbl), Intent(inout), Dimension(ISNRow,ISNCol) :: RMOut
  Character(len=400) :: CSErr
  Integer(ikXL), intent(inout) :: ISStat
  Integer(IkXL) :: c1, c2
  open(unit=200,file=Trim(AdjustL(CSFilename)),status="old",action="&
   &read",form="unformatted",iostat=isstat,iomsg=cserr)
  If(ISSTat==0) Then
    Do c1=1,ISNRow
      read(200,iostat=isstat,iomsg=Cserr) (RMOut(c1,c2),c2=1,ISNCol)
      If(ISStat/=0) Then
        write(*,*) "Reading error"//Trim(AdjustL(CSErr))
        exit
      End If
    End Do
  Else
    write(*,*) "Opening error "//Trim(AdjustL(CSErr))
  End If
  If(ISSTat==0) Then
    write(*,*) "Reading successfull"
  End If
  close(unit=200,status="keep")
End Subroutine readfortran



More information about the R-help mailing list