2

在下面的示例中,我尝试在子例程中释放分配的字段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 TYPE块内f指向无限多态指针field。我的假设是它f的类型是integer,并且应该可以释放数组f指向的点。Ifort 14 不编译此示例并显示错误消息:

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

我假设f将是一个指针,因此它可以被释放。有没有办法以这种任意方式解除分配,或者 Fortran 2003 标准不可能做到这一点?

4

0 回答 0