- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[fortran]MODULE mytype_module integer,save :: icount = 0 TYPE mytype integer,allocatable :: val(:) contains final :: final END TYPE INTERFACE mytype MODULE PROCEDURE int_to_mytype END INTERFACE ! Operator definitions etc. CONTAINS FUNCTION int_to_mytype(i) result(x) TYPE(mytype) :: x INTEGER,INTENT(IN) :: i allocate(x%val(i)) ; x%val = 1 print *,'get 1' END FUNCTION subroutine final(x) type(mytype) :: x icount = icount + 1 print *,'finalized' end subroutine END MODULE PROGRAM example USE mytype_module TYPE(mytype) :: y y = mytype(10) print *,'finalize count',icount END PROGRAM[/fortran]
thne result is:
finalized
finalized
finalize count 2
compiler xe 2011 update1 win 64
if change the component array from allocatable to pointer, then result would be once.
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
if i define assignment
interface assignment(=)
module procedure assb
END INTERFACE
the result is only finalize 1
if i change allocatable component to pointer, the result is 0.
the fortran 2003 reference said
If an executable construct references a function, the result is nalized after execution of the innermost executable construct containing the reference.
but is seems that if a derive type does not have a allocatable component, the function result would not be finalized.
interface assignment(=)
module procedure assb
END INTERFACE
the result is only finalize 1
if i change allocatable component to pointer, the result is 0.
the fortran 2003 reference said
If an executable construct references a function, the result is nalized after execution of the innermost executable construct containing the reference.
but is seems that if a derive type does not have a allocatable component, the function result would not be finalized.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The function int_to_mytype is creating one instance of mytpe then assinging it to your local variable y which is a second instance. These two instances both require finalizing.
When assigning a pointer you only have one instance.
When assigning a pointer you only have one instance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[fortran]MODULE mytype_module
integer,save :: icount = 0
TYPE mytype
integer :: len
! integer,allocatable :: val(:)
integer,pointer :: val(:)
contains
final :: final
! procedure :: assb
! generic :: assignment(=) => assb
END TYPE
interface assignment(=)
module procedure assb
END INTERFACE
INTERFACE mytype
MODULE PROCEDURE int_to_mytype
END INTERFACE
! Operator definitions etc.
CONTAINS
subroutine assb(a,b)
class(mytype),intent(inout) :: a
class(mytype),intent(in) :: b
a % val => b%val
end subroutine
FUNCTION int_to_mytype(i) result(x)
TYPE(mytype) :: x
INTEGER,INTENT(IN) :: i
allocate(x%val(i)) ; x%val = 1
! allocate(x%vp(i)) ; x%vp= -1
END FUNCTION
subroutine final(x)
type(mytype) :: x
icount = icount + 1
print *,'finalized'
end subroutine
END MODULE
PROGRAM example
USE mytype_module
TYPE(mytype) :: y
type(mytype) :: z
integer,allocatable :: b(:)
b = [1,2,3]
y = mytype(10)
print *,'finalize count',icount
z = y
print *,'finalize count',icount
z = int_to_mytype(10)
print *,'finalize count',icount
END PROGRAM
the result is
finalize count 0
finalize count 0
finalize count 0
[/fortran]
so why int_to_mytype does not create a instance when then component of mytype is a pointer instead of a allocatable array

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