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

why a derive type contains allocatable array would finalize twice when use constructer

dumashu
Beginner
766 Views
[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.
0 Kudos
3 Replies
dumashu
Beginner
766 Views
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.
0 Kudos
Andrew_Smith
Valued Contributor I
766 Views
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.
0 Kudos
dumashu
Beginner
766 Views
[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
0 Kudos
Reply