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

Will the Intel Fortran deallocate the subobject automatically?

史_建鑫
Beginner
851 Views



consider the following four conditions:
case 1: allocatable variable contains allocatable subobject
[fortran]
type :: testype2
    type(testype1),allocatable, dimension(:) :: tt1
end type testype2

type(testype2), allocatable, dimension(:) :: tt2
[/fortran]

case 2: allocatable variable contains pointer to subobject
[fortran]
type :: testype2
    type(testype1),pointer, dimension(:) :: tt1
end type testype2

type(testype2), allocatable, dimension(:) :: tt2
[/fortran]

case 3: pointer variable contains allocatable subobject
[fortran]
type :: testype2
    type(testype1),allocatable, dimension(:) :: tt1
end type testype2

type(testype2), pointer, dimension(:) :: tt2
[/fortran]

case 4: pointer variable contains pointer to subobject
[fortran]
type :: testype2
    type(testype1),pointer, dimension(:) :: tt1
end type testype2

type(testype2), pointer, dimension(:) :: tt2
[/fortran]

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!

0 Kudos
3 Replies
IanH
Honored Contributor III
851 Views

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.

0 Kudos
史_建鑫
Beginner
851 Views

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?

0 Kudos
IanH
Honored Contributor III
851 Views

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.

0 Kudos
Reply