- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page