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

Passing an allocated array

efengle
Novice
335 Views

I have an array that is defined to be ALLOCATABLE (defined in my main program).  A subroutine is call and that is where it will be allocated.  So the dummy argument of the subroutine also needs to be ALLOCATABLE.  For this work correctly, the subroutine needs an explicit interface and I have that in a module.

After that, the array will need to be passed into other subroutines.  These subroutines will never be messing around with allocation -- simply using the contents of the array.

Currently, I have the dummy argument for the array in other subroutines sizing the array explicitly.  The scalar that sizes the dummy array argument is the same value that allocated the array.

The question is...is this "safe" to do?  My program compiles and executes successfully, but I'd like to have some opinions and thoughts on the matter.

Thanks!

0 Kudos
3 Replies
mecej4
Honored Contributor III
335 Views

What you are doing is fine.

Just remember that, in the subroutines to which a previously allocated array has been passed as an argument without the ALLOCATABLE attribute, you cannot, for instance, query whether the array has been allocated by using the ALLOCATED intrinsic. If these subroutines do not have other allocatable arguments, or have other features that mandate providing an explicit interface, you do not need the subroutines to be in USEd modules or to provide explicit interfaces.

0 Kudos
jimdempseyatthecove
Honored Contributor III
335 Views

It is safe to do provided that the extents of the dimension are correct in the caller.

An alternative is to use Assumed-Shape specifications... but this requires the use of an explicit interface (generally in a module).

Assumed-Shape may incur a little more overhead in calling. IOW do not use Assumed-Shape on arrays with known dimensions (e.g. a rotational matrix).

Jim Dempsey

0 Kudos
efengle
Novice
335 Views

Thanks for the replies and information!

0 Kudos
Reply