Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deallocation of derived types with local allocatables

Status
Not open for further replies.

GerritGroot

Technical User
Nov 3, 2006
291
ES
Hi,

Imagine that I've got a derived type as follows:
Code:
TYPE MY_Type
   INTEGER :: intone,inttwo
   INTEGER, DIMENSION(:), ALLOCATABLE :: myintarr
   CHARACTER(LEN=80) :: myname
   REAL, DIMENSION(:), ALLOCATABLE :: reallistone,reallisttwo
   REAL, DIMENSION(:,:), ALLOCATABLE :: matone,mattwo
   REAL, DIMENSION(:,:,:), ALLOCATABLE :: cubeone,cubetwo
END TYPE MY_Type
And then an array of this type, say:
Code:
TYPE (MY_Type), DIMENSION(:), ALLOCATABLE :: MY
Then if I first allocate MY
Code:
ALLOCATE(MY(10))
for example and then allocate locally, i.e. something like:
Code:
ALLOCATE(MY(3)%reallistone(7))

Would it be enough to just deallocate the variable MY before closing the program?
Code:
DEALLOCATE(MY)

Or do I have to deallocate all the sub-%parts separately?

It seems to work till now, though when repeating the program I get some strange behaviour and, though I guess not, I wonder whether it has something to do with this.

Thanks,

Gerrit
 
You have to deallocate the sub% parts as well otherwise you get a memory leak.
 
ALLOCATABLE arrays have been designed to avoid any memory leak. So no : you cannot have a problem of memory except if the compiler has a bug !

Even your DEALLOCATE statement is just optional if your main structure MY is local to a subroutine of function : it is automatically deallocated when leaving the procedure.

As conclusion, from my point of view (and I am rather confident with it), your proposal is OK : you may just deallocate the root. The compiler must do the rest!

François Jacq
 
Fjacq, you're right. I stand corrected.
 
Thanks! It is being allocated in a module that exists in te main, however, when repeating the code (by means of a loop in the main without closing the *.exe) I got sometimes some strange errors (sometimes not) that looked like allocation errors(the typical thing when windows says "some error occurred wanna send a report?"

I use gfortran, maybe the compiler should, but does NOT take care of it.

Checking will take me time as I have to leave the code for a while, but as soon as I find something out I'll let you know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top