- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
!----------------------------------------------------------------
! 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!
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
> 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(:)
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(:)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page