Search code examples
macosgfortranallocation

Can gfortran allocate with MOLD or not?


I am trying to compile a fairly modern fortran code on a Mac with gfortran. Lines like this one ...

allocate(sce, mold=sct)

get errors like this:

Error: Array specification or array-valued SOURCE= expression required in ALLOCATE statement

It seems, then, that gfortran understands SOURCE but not MOLD. However, my gfortran is recent:

 gcc version 8.2.0 (MacPorts gcc8 8.2.0_3)

I have seen web pages claiming that gfortran has been able to support MOLD since version 7.something. So surely it should be able to do it with 8.2.0. Is this not true? Is something funny about the Mac version? Can anyone suggest what some other problem might be?

EDIT: If anyone's still there, this toy code works fine:

program awm

integer, dimension(:), allocatable :: sct,sce

integer :: nspec = 100

allocate(sct(nspec))
allocate(sce, mold=sct)

end program

But when sct and sce are derived types, it falls apart:

program awm

type :: r1d
    real, allocatable :: a(:)
end type

type(r1d), dimension(:), allocatable :: sct,sce

integer :: nspec = 100

allocate(sct(nspec))
do i = 1,nspec
  allocate(sct(i)%a(10))
enddo 
allocate(sce, mold=sct)

end program

This returns the error I was having above. You might think the definition of the derived type is kind of weird, even unnecessary. Well, the original code from which this is taken is not my code and I'm not in a position to change it much, and actually, this structuring does have its uses for reasons I don't have time to go into. Thanks.


Solution

  • I am convinced this is a duplicate of this GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80708 or is very closely related. You have to wait for a fix. I will put your code in the comment for this bug and not open a new one.