fortran - Proper way to pass pointers into many subroutines -
i'm not programmer , i'm trying interface model provides data pointers. these pointers passed down through several subroutines before data written them. i'm not sure how avoid memory leaks.
let's have array pointer a
passed several subroutines before being written to, how handle declarations, allocations, , deallocations?
module data implicit none contains subroutine s1(a) real, pointer, intent(out) :: a(5,5) call s2(a) end subroutine s1 subroutine s2(a) real, pointer, intent(out) :: a(5,5) integer :: = 1,5 a(:,i) = 5.0 end end subroutine s2 end module data program test use data, : s1, s2 real, pointer, dimension(:,:) :: => null() allocate(a(5,5)) call s1(a) write(*,*) deallocate(a) end program test
please note code not fortran 90. intent
attribute dummy (formal) arguments pointers introduced in fortran 2003.
the intent
refers association status of pointer, not target. also, if argument derived type pointer components, intent
applies type object itself, not targets of pointers. is, if, example, intent(in)
used, data area pointer targeted @ can modified:
module mytype_mod implicit none private type, public :: mytype integer, pointer :: ptr(:) contains procedure :: sub => my_type_sub end type mytype contains subroutine my_type_sub(self) ! dummy argument class(mytype), intent(in) :: self ! following legal, ! though intent(in) specified self%ptr = 42 end subroutine my_type_sub end module mytype_mod program main use mytype_mod, only: & mytype implicit none type(mytype) :: foo integer :: alloc_stat allocate( integer :: foo%ptr(100), stat=alloc_stat ) call foo%sub() end program main
even though not required, in case such previous example, better state intent(inout)
indicate reader modification of data taking place.
on note, may find answer useful fortran subroutine returning wrong values
Comments
Post a Comment