Reputation:
This is my code:
Program Dynamic_Array
Use Variables
Use Allocation_Module
Use Dealloaction_Module
Implicit none
Call Subroutine_0
Call Subroutine_1
End Program Dynamic_Array
Module Variables
Implicit none
Integer :: i , k
Integer , parameter :: Br_sn_cvo = 10
Integer , parameter :: Br_nn_mre = 7
Integer , parameter , dimension ( Br_nn_mre) :: Br_nn_cvo = [ 7 , 6 , 5 , 4 , 3 , 2 , 1 ]
Integer , dimension ( Br_nn_mre ) :: i_nn_dm_1 , i_nn_dm_2
type :: my_type
integer, allocatable :: my_size(:)
end type my_type
type(my_type), allocatable :: dS_sn(:)
End Module Variables
Module Allocation_Module
Use Variables
Implicit none
Contains
Subroutine Subroutine_0
Allocate(dS_sn(Br_nn_mre))
Loop_1: Do k = 1, Br_nn_mre
i_nn_dm_1(k) = Br_sn_cvo + Br_nn_mre + 1 + Br_nn_cvo(k) * ( k - 1 )
i_nn_dm_2(k) = Br_sn_cvo + Br_nn_mre + k * Br_nn_cvo(k)
Allocate( dS_sn(k)%my_size( i_nn_dm_1(k) : i_nn_dm_2(k)) )
Loop_2: Do i = i_nn_dm_1(k) , i_nn_dm_2(k)
dS_sn(k)%my_size(i) = i + k
End Do Loop_2
End do loop_1
End subroutine Subroutine_0
End Module Allocation_Module
Module Dealloaction_Module
Use Variables
Implicit none
Contains
Subroutine Subroutine_1
Do k = 1 , Br_nn_mre
Deallocate(dS_sn(k)%my_size)
End do
Deallocate(dS_sn)
Return
End Subroutine Subroutine_1
End Module Dealloaction_Module
I am not experienced programer in Fortran so I need to ask a few questions about process of memory allocation and deallocation for a dynamic arrays. Is there any problem with memory leak in this code? Is this correct way for a memory allocation in separate module? Is this correct way for a memory deallocation in separate module?
Upvotes: 0
Views: 946
Reputation: 724
Here is an example...
...
IF(ALLOCATED(TheArray)) THEN
IF(SIZE(TheArray) /= The_Size_I_need) DEALLOCATE(TheArray)
ENDIF
IF(.NOT. ALLOCATED(TheArray)) ALLOCATE(TheArray(The_Size_I_need))
This is useful if the array gets used repeatedly for different processing sizes.
If it is "always" fixed in terms of the current execution, then there is no real need to do anything.
Upvotes: 1
Reputation: 60008
There is no memory leak in the code. It is impossible to make a memory leak with allocatable
entities in Fortran. Only pointer
can cause a memory leak.
With allocatable if something is going out of scope, it is deallocated automatically.
Your main array is a module variable so it is never going out of scope (it is save
implicitly by Fortran 2008 rules). So if you don't deallocate it yourself, it will remain allocated and then deleted by the operating system on the program termination. But that is not normally considered to be a memory leak. It is not really harmful, because there is no way to make some forgotten copies of the array in memory.
The individual components my_size
could go out of scope, when deallocating the large array dS_sn
. In that case they are deallocated automatically by Fortran rules. You don't have to deallocate them one by one.
So you do not really have to do
Do k = 1 , Br_nn_mre
Deallocate(dS_sn(k)%my_size)
End do
Doing just
Deallocate(dS_sn)
is perfectly correct.
Upvotes: 0