Print

Print


[log in to unmask] wrote:
> 
> 2) The result is an array:
>         integer, dimension(2) :: B
>         B = MYPACK(A, Mask)
> 
> This is valid FORTRAN.  However, MYPACK had to allocate an array to
> hold the result.  That result is then copied into the user array B.
> The memory that was allocated inside MYPACK is lost.

I still believe, the compiler should take care of that pointer function 
if an assignment action has been done by the compiler.   

The following solution should work for case 2.  You might even expand it 
to all arithematic operations with the function, if you don't care to 
define all operators youself, to make it as applicable as PACK().

Jing

module m_pfun
 type rank1fun          ! only for function definitions
   real,pointer,dimension(:) :: p
 end type rank1fun
 interface assignment(=); module procedure aeqp; end interface
contains
subroutine aeqp(a,h)
  real,dimension(:),intent(out) :: a
  type(rank1fun),intent(in) :: h
  a(:)=h%p(:)
  deallocate(h%p)
  print*,'dealloc'
end subroutine aeqp
function pfun(n)
  type(rank1fun) :: pfun
  print*,'alloc'
  allocate(pfun%p(n))
  pfun%p(:)=(/(i-1,i=1,n)/)
end function pfun
end module m_pfun

program test
  use m_pfun
  real,dimension(3) :: a
  a=pfun(size(a))
  print*,a
end program test

-- 
________________________________ _-__-_-_ _-___---
Jing Guo, [log in to unmask], (301)805-8333(o), (301)805-7960(fx)
Data Assimilation Office, Code 910.3, NASA/GSFC, Greenbelt, MD 20771


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%