integer ary1(:), ary11(:, :,
allocatable ary1, ary11 !第一种声明动态数组的方式
integer, allocatable:: ary2(:), ary22(:) !第二种声明动态数组的方式
integer, dimension(:), allocatable:: ary3, ary33 !第三种声明动态数组的方式
!下面是测试部分
allocate(ary1(1:5), ary11(3, 3, 3), stat = err_mesg)
if(0 .eq. err_mesg) then
print *, "ary1 initialize successed!"
else if(0 .ne. err_mesg) then
print *, "ary1 initialize failed", err_mesg
end if !以上测试正常分配的情况,分配成功stat = 0
!查看动态数组是否分配空间
if(.not. allocated(ary2)) print *, 'not allocated ary2'
if(.false. .eq. allocated(ary22)) print *, 'not allocated ary22'
if(.true. .eq. allocated(ary1)) print *, 'allocated ary1'
!已被分配空间的数组不可重复分配
allocate(ary11(2, 2, 2), stat = err_mesg)
if(0 .ne. err_mesg) print *, 'Ooops, ary11 has been allocated'
!内存的释放,只有被allocate的内存才能用deallocate释放,否则报错
deallocate(ary1, stat = err_mesg)
if(0 .ne. err_mesg) then
print *, 'ary1 is not allocated'
else if(0 .eq. err_mesg) then
print *, 'ary1 del successfully'
end if
deallocate(ary3, stat = err_mesg)
if(0 .ne. err_mesg) then
print *, 'ary3 is not allocated'
else if(0 .eq. err_mesg) then
print *, 'ary3 del successfully'
end if
end
subroutine disp(ary)
parameter (row = 5)
!integer row
integer:: ary(1:row)
do i = 1, row
print *, ary(i)
end do
end
上述程序的输出:
ary1 initialize successed!
not allocated ary2
not allocated ary22
allocated ary1
Ooops, ary11 has been allocated
ary1 del successfully
ary3 is not allocated
请按任意键继续. . .