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

Operator Assignment

dannycat
New Contributor I
363 Views
I am trying to write a procedure that will perform the operation of copying one allocatable array to another. I could use the intrinsic array ArrayA = ArrayB but I need to check if arrayA is already allocated and if not allocate it. Unfortunately when I code this in a module I get an error because I am effectively trying to recreate the intrinsic function.

Is there a neat way of getting round this or am I confined to calling the copy procedure as a subroutine?

This is the procedure coded up.

module NameFlags

!*********************************************************************
! Name Flags
!*********************************************************************

implicit none

interface assignment (=)
module procedure nm_CopyFlg
end interface

contains

subroutine nm_CopyFlg(flgo,flgin)

!**********************************************************
! Copy Flag Array
!**********************************************************

implicit none

! Arguments
integer(2),allocatable,intent(inout) :: flgo(:)
integer(2),allocatable,intent(in) :: flgin(:)

! Local variables
integer :: ubout
integer :: ubin
integer :: ier

! Initialise
ier = 1

! Check size of output flag array
ubin = ubound(flgin,1)
if(allocated(flgo)) then

! Don't reallocate if it's the same size
ubout = ubound(flgo,1)
if(ubin.ne.ubout) then
deallocate(flgo)
allocate(flgo(ubin),stat=ier)
else
ier = 0
endif

else
allocate(flgo(ubin),stat=ier)
endif

! Copy data
if(ier.eq.0) then
flgo = flgin ! Group Flags
endif

return
end subroutine

end module

Perhaps I am asking too much but it would be much neater to write:

pln%flg = pln1%flg

rather than

call nm_CopyFlg(pln%flg,pln1%flg)




0 Kudos
1 Reply
Steven_L_Intel1
Employee
363 Views
Use the intrinsic assignment and compile with the /assume:realloc_lhs option (you'll have to type this in under Command Line > Additional Options.) This will give you the F2003 semantics on array assignment where an allocatable array will be [re]allocated if necessary.
0 Kudos
Reply