allocated
在 fortran 中,可以使用以下语句检查是否分配了可分配数组:
program test_allocated
integer :: i = 4
real(4), allocatable :: x(:)
print *, 'before allocation of x'
print *, 'allocated(x)'
print *, allocated(x)
allocate(x(i))
print *, 'after allocation of x'
print *, 'allocated(x)'
print *, allocated(x)
end program test_allocated
但是,是否也可以以某种方式检查常规数组是否被声明为可分配的?以下代码
program test_allocated
integer :: i = 4
real(4), allocatable :: x(:)
real(4)::y
print *, 'before allocation of x'
print *, 'allocated(x)'
print *, allocated(x)
print *, 'allocated(y)'
print *, allocated(y)
allocate(x(i))
print *, 'after allocation of x'
print *, 'allocated(x)'
print *, allocated(x)
print *, 'allocated(y)'
print *, allocated(y)
end program test_allocated
抛出错误
D:\TEMP\FortranTest\ifort>ifort test.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Inte
l(R) 64, Version 13.0.1.119 Build 20121008
Copyright (C) 1985-2012 Intel Corporation. All rights reserved.
test.f90(9): error #6547: The ARRAY argument of the ALLOCATED inquiry intrinsic
function shall be an allocatable array. [Y]
print *, allocated(y)
---------------------^
test.f90(15): error #6547: The ARRAY argument of the ALLOCATED inquiry intrinsic
function shall be an allocatable array. [Y]
print *, allocated(y)
---------------------^
compilation aborted for test.f90 (code 1)
也许存在另一种查询数组性质的语句或方法?