Recursive function with allocatable array

michael baudin michael.baudin@gmail.com
Wed Apr 16 15:33:00 GMT 2008


Hi,

I encountered a problem with gfortran while developing a recursive function
which allocates an array stored in a derived-type.
This is the sample test :

module testmod

#define _DYNAMIC_TYPE allocatable
!#define _DYNAMIC_TYPE pointer

  type, public :: t_type
     integer, dimension(:), _DYNAMIC_TYPE :: chars
  end type t_type
  integer, save :: callnb = 0
contains
  recursive function recursivefunc ( this ) result ( match )
    type(t_type), intent(in) :: this
    type(t_type) :: subpattern
    logical :: thisalloc
    integer :: thislength
    logical :: match
    write ( * , * ) "recursivefunc [" , callnb , "]"
    thislength = size ( this % chars )
    write ( * , * ) "length :", thislength
    callnb = callnb + 1
    thisalloc = allocated ( this % chars )
!    thisalloc = associated ( this % chars )
    if ( .NOT. thisalloc ) then
       write ( 6 , * ) "STOP with error !"
       stop
    endif
    if ( thislength == 0 ) then
       match = .true.
       return
    endif
    allocate ( subpattern % chars ( thislength - 1 ) )
    match = recursivefunc ( subpattern )
  end function recursivefunc
end module testmod

program testprog
  use testmod
  implicit none
  type(t_type) :: this
  logical :: match
  allocate ( this % chars ( 10 ))
  match = recursivefunc ( this )
  print * , "match :", match
end program testprog

Compile it with :

gfortran -x f95-cpp-input testZeroSizeRecursive2.f90 -o
testZeroSizeRecursive2.exe
(flibs-workbench) 60 % testZeroSizeRecursive2.exe
 recursivefunc [           0 ]
 length :          10
 recursivefunc [           1 ]
 length :           9
 STOP with error !

This is because the allocated statement return .false., even
 if the array is really allocated.
Trying the "pointer" version by modifying the #define preprocessing statement
and the "allocated" to associated" make the problem disappear :

 recursivefunc [           0 ]
 length :          10
 recursivefunc [           1 ]
 length :           9
 recursivefunc [           2 ]
 length :           8
 recursivefunc [           3 ]
 length :           7
 recursivefunc [           4 ]
 length :           6
 recursivefunc [           5 ]
 length :           5
 recursivefunc [           6 ]
 length :           4
 recursivefunc [           7 ]
 length :           3
 recursivefunc [           8 ]
 length :           2
 recursivefunc [           9 ]
 length :           1
 recursivefunc [          10 ]
 length :           0
 match : T

This is the version of gfortran I currently use ( I downloaded it today
and I think that it is up-todate ) :

(flibs-workbench) 61 % gfortran -v
Using built-in specs.
Target: i586-pc-mingw32
Configured with: ../trunk/configure --prefix=/mingw
--enable-languages=c,fortran --with-gmp=/home/FX/local
--with-ld=/mingw/bin/ld --with-as=/mingw/bin/as --disable-werror
--enable-bootstrap --enable-threads --disable-nls
--build=i586-pc-mingw32 --enable-libgomp --disable-shared
Thread model: win32
gcc version 4.4.0 20080312 (experimental) [trunk revision 133139] (GCC)

My conclusion is that it really is a bug in gfortran.
What is you point of view ?

Best regards,

Michaël



More information about the Fortran mailing list