[Rd] ALLOCATE in a FORTRAN subroutine

Duncan Murdoch murdoch.duncan at gmail.com
Fri Mar 4 12:40:51 CET 2016


On 04/03/2016 3:34 AM, MAURICE Jean - externe wrote:
> Hi,
> I am a FORTRAN developer and I am 'translating' R functions in FORTRAN subroutines. I am 'new' to R. It's my first question in this mailing-list and English is not my natural language.
>
> Very often, an R function gives an  'array' as result and you don't have to bother with the dimension of the array : R creates automatically an array with the good length. It's not really the case with FORTRAN. I call FORTRAN subroutines with .fortran().
>
> Until now, I create an array with the 'max' dimensions in R, give it to FORTRAN; FORTRAN updates the array and R retrieves it. But calculating the 'max' before calling the FORTRAN subroutine can be complicated. Is it possible to create a 'new' array in a FORTRAN subroutine and to make it be read by R ?

I don't think this is possible in pure Fortran, but it is certainly 
possible in C or C++ code.  You need to call the external routine using 
.Call() instead of .C or .Fortran.  See the section 5.9 of the Writing R 
Extensions manual for details.

> Or is it possible to have a 'pointer' in R, to give it to the FORTRAN subroutine where an ALLOCATE can create the array and then R works with the array ?

No.  R needs to manage allocations of all the objects it uses.  However, 
if you never need to use these arrays in R code (you just want R to keep 
references to them to pass to other external routines), you can (in C or 
C++) use the external pointer type.  I don't think there's any support 
for that in Fortran.

> The other solution, is to work with dummies dimension in FORTRAN (REAL*8 array1(*)) but can R work with that ?

No, for the same reason.

Duncan Murdoch



More information about the R-devel mailing list