I'm trying to use structured variable with allocatable components (beginner in this area). I have doubt aboutfree upmemory.Could somebody help me to explainwhen I need to deallocate the array by myself. I put short sample of code for illustration.
1) I'm allocating local array inside the subroutine or function.I suppose that the local arrays allocatedare clean(deallocated) automatically on exit from procedure ifthey don't have the attribute save. It is right?
2) Type Struct contains allocatable array S of type S1. The type S1 also contains allocatablearray of real (see please sample code). WhenI deallocate the array inside the structure Struct.What happenswith allocated array inside the structure S1? Is the memorycleaned automatically or the array is wasting there? Should I deallocate firstly all arrays in S1 and then in Struct?
Type(struct) :: Mobj
deallocate(Mobj.s) ?? it is enough
3) I have eg.functionModifyObject_1 modifying some template object and creating new modified object ( see please the sample code). I'm using Move_alloc procedure for changing the array inside the Struct which should save time and should clean the allocated array. It is OK?
4)What happens with allocated memory for Mobj when I try to write following statement in my code
It is memory free automatically orI should deallocate arrays inMobjinside function?
5) The last situation not clear for me. What happens in the case when I have eg. two variables
type (Struct) :: Mobj1,Mobj2
I allocated arrays insideMobj1 and Mobj2 (havingdifferent dimension I think not important)and now I will write statement Mobj1=Mobj2. I hope that the compiler solving this and the initially allocated array in Mobj1 are deallocated andI don't need to do it by myself.It is right?
Thank you in advance for your comments and remarks.
integer :: Nx=0
real*8,allocatable :: X(:)
!...... more components
integer :: ns=0
type(s1), allocatable :: s(:)
! ...... more components
module procedure ModifyObject_1
function ModifyObject_1 (obj, s) result(obj1)
type(struct), intent(in) :: obj
type(S1), intent(in) :: s
type(struct) :: obj1
type(S1),allocatable :: SA(:)
if (Obj%Ns>0) SA(:Obj%Ns)=Obj%s(:Obj%Ns)
! --------------------- test program ----------
! test samples
Type(struct):: MObj, Mobj1
type(S1), allocatable :: Seg
integer :: Nx