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

variable type in out a subroutine

diedro
Beginner
1,610 Views

dear all,

I would like to know how works the allocatable variable type in and out a subroutine

This is my problem.

I have this variable type:

  TYPE tParticle
    SEQUENCE
    INTEGER               :: ip
    INTEGER               :: mc
    INTEGER               :: bcflag
    INTEGER               :: cpu
    REAL    ,ALLOCATABLE  :: RP(:)
    REAL    ,ALLOCATABLE  :: QQ(:)
    REAL    ,ALLOCATABLE  :: UU0(:)
    REAL                  :: lij
    REAL                  :: lmls
    REAL                  :: vol
  ENDTYPE tParticle

and I allocate it in the following way:

  ALLOCATE (PART          (nptPbuffer))

 DO ip=1,nptPbuffer
     ALLOCATE(PART(ip)%RP(di))
     ALLOCATE(PART(ip)%QQ(nVar))
     ALLOCATE(PART(ip)%UU0(countBufferRK))
ENDDO

Here the problem: how can I pass the variable type?

Here an example:

SUBROUTINE SPH(nptTot,PART)
 TYPE(tParticle),DIMENSION(nptTot)      ,INTENT(IN)  :: PART

is it correct?

How can I deallocate the variable type? Can I simple do:

DEALLOCATE(PART)

thank to all of you

0 Kudos
1 Reply
mecej4
Honored Contributor III
1,610 Views

To answer your question comprehensively would require many pages of narrative and numerous extracts from the Fortran standard. Let us, therefore, consider a simpler case: you wish to pass as actual arguments to a subroutine objects that are allocated variables or derived type variables with one or more allocatable components, and the subroutine is such that none of these allocatable variables/components will be deallocated, allocated or reallocated or queried as to allocation status. In this case, you can generally write the subroutine code as if the arguments were ordinary (i.e., non-allocatable) variables.

If, on the other hand, you have a subroutine in which the allocation status of one or more of its dummy arguments is going to be changed or enquired about, you have to declare those variables ALLOCATABLE, and the caller must have been provided an explicit interface to the subroutine. The rules in this case are more complex, and can be different in Fortran 95 versus in Fortran 2003.  You will need to do a good bit of reading before writing code requiring this property. In general, when a local variable or a component is allocatable, it/they becomes deallocated automatically when the subroutine is exited, unless it is not safe to do so.

Although the rules may at first look appear daunting to read, your subroutine code will be much safer and error-free if written with allocatable variables and components than with POINTER and TARGET attributes.

 

0 Kudos
Reply