Will the Intel Fortran deallocate the subobject automatically?

Will the Intel Fortran deallocate the subobject automatically?

consider the following four conditions:
case 1: allocatable variable contains allocatable subobject

type :: testype2
    type(testype1),allocatable, dimension(:) :: tt1
end type testype2
type(testype2), allocatable, dimension(:) :: tt2

case 2: allocatable variable contains pointer to subobject

type :: testype2
    type(testype1),pointer, dimension(:) :: tt1
end type testype2
type(testype2), allocatable, dimension(:) :: tt2

case 3: pointer variable contains allocatable subobject

type :: testype2
    type(testype1),allocatable, dimension(:) :: tt1
end type testype2
type(testype2), pointer, dimension(:) :: tt2

case 4: pointer variable contains pointer to subobject

type :: testype2
    type(testype1),pointer, dimension(:) :: tt1
end type testype2
type(testype2), pointer, dimension(:) :: tt2

if I want to deallocate variable tt2, will all the space tt1 occupied will be free? or should i do something in the testype2 final procedure?

Thank you for your help!

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

case 1: The component tt1 will be automatically deallocated when the variable tt2 is deallocated.  The variable tt2 will be automatically deallocated where applicable.

case 2: The component tt1 must be manually deallocated if it was previously allocated, before the variable tt2 is deallocated.  The variable tt2 will be automatically deallocated where applicable.

case 3: The component tt1 will be automatically deallocated when the variable tt2 is deallocated.  The variable tt2 must be manually deallocated if it was previously allocated.

case 4: The component tt1 must be manually deallocated if it was previously allocated, before the variable tt2 is deallocated.  The variable tt2 must be manually deallocated if it was previously allocated.

Allocatables - components automatically deallocated when the structure is deallocated, non-saved locals automatically deallocated when they go out of scope, INTENT(OUT) dummy arguments deallocated at procedure start.  They do not leak.

Pointers - you must manage components, you must manage variables.  Leaks are possible.  Note that a pointer may be pointed at any compatible target, not just something that is the result of an allocate statement.

Thanks lanH.

So in case 2, i should write a final procedure for testype2, and deallocate the tt1 in the final procedure. is this the best and common way to manager subobject memory space?

If the component doesn't need to be a pointer (does it get pointed at things that already exist?) then I would not make it a pointer.  Allocatables are safer in many ways. 

It the component has to be a pointer, then it will one day (in the future) be a common way, once compiler support is sufficient.  Finalization is a F2003 feature.  Right now I'd manually manage the deallocation.

Leave a Comment

Please sign in to add a comment. Not a member? Join today