在SELECT TYPE块中取消分配

时间:2016-05-02 07:48:27

标签: fortran fortran2003

在以下示例中,我尝试在子例程deallocate_arbitrary中释放已分配的字段。这应该适用于多种类型的无限多态指针,但在该示例中,仅使用type is (integer)定义整数数组的版本。

module mymod
    IMPLICIT NONE

    contains

    subroutine deallocate_arbitrary(field)
        class(*), dimension(:), pointer, intent(inout) :: field
        logical :: deallocatable

        deallocatable = .false.

        select type(f => field)
        type is (integer)
            print *, 'deallocate_arbitrary :', f
            deallocate(f)
        end select
    end subroutine deallocate_arbitrary

end module mymod

program playground

    use mymod, only : deallocate_arbitrary

    IMPLICIT NONE

    integer, allocatable, target :: ints(:)
    class(*), dimension(:), pointer :: pints
    integer :: i

    ints = (/ (i, i = 1, 3) /)
    print*, 'ints: ', ints
    pints => ints
    call deallocate_arbitrary(pints)
    print *, 'ints2: ', ints

end program playground

SELECT TYPEf内指向无限多态指针field。我的假设是f的类型为integer,并且应该可以将数组f解除分配。 Ifort 14没有使用错误消息编译此示例:

error #6724: An allocate/deallocate object must have the ALLOCATABLE or POINTER attribute.

我假设f是一个指针,因此可以解除分配。有没有办法以这种任意方式解除分配,或者使用Fortran 2003标准是不是可能?

0 个答案:

没有答案