Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29435 討論

question about free user defined allocatable array

haijunwu
初學者
936 檢視
Hi, I have a quesion about free user defined allocatable array. Followings are sample codes of my quesion.

!----------------------------------------------------------------
! This is the user defined data type
type :: VarType
real(8),allocatable :: Var_inner(:)
end type

! declare an dynamic array
type(VarType), allocatable :: Var_outer(:)

! allocate the array and its inner arrays
allocate(Var_Outer(10))
do i = 1, 10
allocate(Var_outer(i)%Var_inner(10))
enddo

! free the array
deallocate(Var_outer)
!----------------------------------------------------------

Quesion is : will it cause memory leak if I just free the outer array?

Thanks in advance!
0 積分
2 回應
mecej4
榮譽貢獻者 III
936 檢視
> will it cause memory leak if I just free the outer array?

In Fortran 95 and later, local allocatable variables are freed when the subprogram is exited. In Fortran 90, memory leaks can be expected unless explicit deallocation is performed.

Correction to Line-8:

type(VarType), allocatable :: Var_outer(:)

Steven_L_Intel1
936 檢視
If you have an allocatable array of a derived type (which you do), and that type has allocatable components (which yours does), deallocating the outer array automatically deallocates the components.
回覆