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

question about free user defined allocatable array

haijunwu
Beginner
267 Views
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 Kudos
2 Replies
mecej4
Honored Contributor III
267 Views
> 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 Kudos
Steven_L_Intel1
Employee
267 Views
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 Kudos
Reply