您的位置:首页 > 其它

fortran F90动态数组的基本使用示范

2011-03-15 21:28 92 查看
program sample
parameter (row = 5)
integer err_mesg

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
请按任意键继续. . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: