第 3 楼
动态数组使用例子:
点击浏览该文件program test_allocate
!(1)声明动态数组。如:real, allocatable, dimension(:)::x
!(2)给动态数组分配内存。如:allocate (x(N))
!(3)将分配的内存释放掉。如:deallocate (x)
!
!在使用动态数组时,要增加动态数组的大小,需将原来的动态数组释放掉,再重新指定动态数组的大小。
!
!动态数组不能作为例程虚参来使用。
!
implicit none
integer ::N,M
real(8),dimension(10) ::ARROR0 !固定数组
real(8),allocatable,dimension(:) ::ARROR1 !声明动态数组
write(*,*) '输入整形变量n和m=?'
read(*,*) N,M
allocate (ARROR1(N)) !给动态数组分配内存
ARROR1=0.9D0
print *, ARROR1
deallocate (ARROR1) !将分配的内存释放掉
call sub1(ARROR0)
print *, ARROR0
allocate (ARROR1(M))
!如果动态数组不分配内存,是不对的
call sub1(ARROR1)
print *, ARROR1
deallocate (ARROR1)
stop 'test_allocate ok!'
contains
subroutine sub1(ARR2)
implicit none
real(8),dimension(:) ::ARR2
ARR2=9.0d0
end subroutine sub1
end program test_allocate
石间野草