Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
公告
Important Update: Community Platform Migration​. Learn more​>
29654 讨论

question about free user defined allocatable array

haijunwu
初学者
1,883 次查看
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
1,883 次查看
> 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(:)

0 项奖励
Steven_L_Intel1
1,883 次查看
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.
0 项奖励
回复