在子程序中分配数组并将其传回时,FORTRAN中的内存泄漏问题 [英] Memory leakage issue in FORTRAN when allocating an array inside a subroutine and passing it back
问题描述
我使用指针将一些数组传递给子例程,然后在该子例程内分配该数组并将其发送回第一个子例程。在一个模块中,我有这样的东西:
module call_test
子程序bla
使用test
双精度,维(:),指针:: xyz
!
接口boink
子程序boink(a)
隐式无
双精度,维(:),指针:: a
结束子程序boink
结束接口boink
!
调用boink(xyz)
deallocate(xyz)
结束子程序bla
结束模块call_test
和另一个模块中我有:
模块测试
包含
子程序boink(a)
隐式无
双精度,维(:),指针:: a
分配(a(10))
结束子程序boink
结束模块测试
它可以正常工作,但问题是每次执行此过程,即调用子程序bla多次,我正在分配一些不会被释放的内存,这会导致内存问题。在第一个模块中使用它之后,有没有什么办法可以在第二个模块中释放数组a?解析方案
bla 调用boink`whichs分配一个指针数组。然后bla1释放它。没有内存泄漏。这两个过程对同一个指针数组使用不同的名称并不重要。
为什么在模块call_test中有一个boink接口?模块使用
s模块test,其中包含boink? 使用
将使模块call_test中已知的boink接口成为已知。
I am using pointers to pass some arrays to a subroutine and then allocate that array inside that subroutine and send it back to the first subroutine. In one module I have something like this:
module call_test
subroutine bla
use test
double precision, dimension(:), pointer :: xyz
!
interface boink
subroutine boink(a)
implicit none
double precision, dimension(:), pointer :: a
end subroutine boink
end interface boink
!
call boink(xyz)
deallocate(xyz)
end subroutine bla
end module call_test
and in another module I have:
module test
contains
subroutine boink(a)
implicit none
double precision, dimension(:), pointer:: a
allocate(a(10))
end subroutine boink
end module test
It works fine, but the problem is by doing this process each time, i.e. calling the subroutine bla many times, I am allocating some memory that won't be deallocated which causes me memory issues. Is there any way to deallocate the array "a" in the second module after using it in the first module?
"bla" calls "boink"` whichs allocates a pointer array. Then "bla1" deallocates it. There is no memory leak. It doesn't matter that the two procedures use different names for the same pointer array.
Why do you have an interface for "boink" in module "call_test" when that module use
s module "test", which contains "boink"? The use
will make the interface of "boink" known in module "call_test".
这篇关于在子程序中分配数组并将其传回时,FORTRAN中的内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!