有哪个Fortran高手指点一下为什么调试程序出错

moduleaimplicitnoneinteger,allocatable::spin(:)integer::M,lendmoduleaprogramtestuseai... module a
implicit none
integer,allocatable::spin(:)
integer::M,l
end module a

program test
use a
implicit none
integer::i,n
real ::harvest
M=0
print *,'input the length of the lain'
read *,l
allocate (spin(l))
call random_number(harvest)
do i=1,l
n=int(l*harvest)
if(harvest<l/2)then
spin(i)=1
else
spin(i)=-1
endif
enddo
call calculate_magnet
print '(a,i5)','the magnet of the lain is ',M

contains

subroutine calculate_magnet
use a
implicit none
allocate(spin(l))
do i=1,l
M=M+spin(i)
enddo
end subroutine calculate_magnet
end program test
展开
 我来答
xfig
2011-03-12 · TA获得超过446个赞
知道小有建树答主
回答量:85
采纳率:0%
帮助的人:0
展开全部
注释掉子函数calculate_magnet中的allocate(spin(l)),因为这是重复分配。
同时在主函数中加入deallocate(spin),避免内存泄漏。
程序如下:

module a
implicit none
integer,allocatable::spin(:)
integer::M,l
end module a

program test
use a
implicit none
integer::i,n
real ::harvest
M=0
print *,'input the length of the lain'
read *,l
allocate (spin(l))
call random_number(harvest)
do i=1,l
n=int(l*harvest)
if(harvest<l/2)then
spin(i)=1
else
spin(i)=-1
end if
end do
call calculate_magnet
print '(a,i5)','the magnet of the lain is ',M
deallocate(spin)
contains

subroutine calculate_magnet
use a
implicit none
! allocate(spin(l))
do i=1,l
M=M+spin(i)
end do
end subroutine calculate_magnet
end program test
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式