可以在子例程的主体中取消分配和分配Fortran子例程的输入参数吗?

时间:2012-12-10 22:08:28

标签: arrays fortran allocation subroutine

更新:我修改过的代码如下所示:

program run_module_test
    use module_test
    implicit none   

    TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)

    call update(xyzArray)
    write(6,*)'xyzArray',xyzArray

end program run_module_test

module module_test
    implicit none

    TYPE :: newXYZ
        real(4) :: x, u
        real(4) :: y, v
        real(4) :: z, w
        real(4),dimension(3) :: uvw     
    END TYPE

    integer(4) :: shape = 3

contains

    subroutine update(xyzArray)

    integer(4) :: i
    TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
    allocate( xyzArray(shape) ) 

    do i = 1, shape
        xyzArray(i)%x = 0
        xyzArray(i)%y = 0
        xyzArray(i)%z = 0
        xyzArray(i)%u = 0
        xyzArray(i)%v = 0
        xyzArray(i)%w = 0
        xyzArray(i)%uvw = (/0,0,0/)
    end do
    return
    end subroutine update

end module module_test

编译时,它们会产生类似的错误:

 TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
                                                    1
Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1)

当我消除update()子例程中的参数时,我收到一个矛盾的错误:

TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
                                                       1
Error: Symbol at (1) is not a DUMMY variable

我是否消除了有用建议中指出的错误来源?这可能是编译器相关的错误(使用mpi90)?

~~~首先编辑~~~ 我有一个子程序,其输入参数是用户定义类型XYZ的数组。我希望解除分配xyzArray并在子例程的主体中将其分配/修改为不同的大小。我尝试了changing array dimensions in fortran建议的方法,但是当我执行以下操作时:

subroutine update(xyzArray, ...)
...
TYPE (XYZ), allocatable :: xyzArray(:)

我收到一条错误消息:

Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1)

当我尝试:

subroutine update(xyzArray, ...)
...
deallocate( xyzArray(myshape) )
allocate( xyzArray(newshape) )

我收到错误消息:

Error: Expression in DEALLOCATE statement at (1) must be ALLOCATABLE or a POINTER
Error: Expression in ALLOCATE statement at (1) must be ALLOCATABLE or a POINTER

在子程序中更改数组大小需要做什么?

2 个答案:

答案 0 :(得分:5)

要做到这一点:

  • 伪参数必须是可分配的。可分配的伪参数需要一个编译器来实现Fortran 2003标准的相关部分(或实现所谓的“可分配”TR的Fortran 95编译器)。

  • 需要显示该过程的显式接口(该过程必须是模块过程,内部过程或在调用范围内具有接口块)。

  • 伪参数不能是intent(in)。如果您在子例程中根本没有使用分配状态或伪参数值的其他方面,则intent(out)可能是合适的(如果事先分配,则在调用过程时将自动释放伪参数),否则意图(inout)或无意图。

(您的第二个示例代码块与deallocate语句有语法错误 - 您应该只指定xyzArray变量,不要使用(myshape)形状规范)

例如,在模块中:

subroutine update(xyzArray)
  type(xyz), allocatable, intent(inout) :: xyzArray(:)
  ...
  if (allocated(xyzArray)) deallocate(xyzArray)
  allocate(xyzArray(newshape))
  ...

答案 1 :(得分:3)

如果您确定要在子例程中释放数组,则可以将伪参数声明为intent(out),以便在输入子例程时自动解除分配:

module whatever
  implicit none

  type :: xyz
    :
  end type xyz

  contains

    subroutine update(xyzArray)
      type(xyz), allocatable, intent(out) :: xyzArray(:)
      :
      allocate(xyzArray(someshape))
      :
    end subroutine update

  end module whatever

正如IanH已经指出的那样,进程必须有一个显式接口(例如包含在模块中),而在调用程序中你必须声明实际的可分配参数:

program test
  use whatever
  implicit none

  type(xyz), allocatable :: array(:)
  :
  call update(array)
  :
end program test