
有哪个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 展开
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 展开
1个回答
展开全部
注释掉子函数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
同时在主函数中加入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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询