On 23/06/2014 10:06, Karl May wrote:
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.

See the 'Writing R Extensions' manual, http://cran.r-project.org/doc/manuals/r-patched/R-exts.html#Debugging

Such things are usually caused by either array overruns or use of uninitialized memory.



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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Please do. This is not reproducible, you have not told us your platform and this is the wrong list ....

--
Brian D. Ripley,                  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to