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

Declaring array in subroutine based on passed argument for size

Chris_H_3
Beginner
392 Views

TWIMC

I was given a program that had this subroutine:

subroutine writePointsNMBGA2(fileName,x, y, values, npts, metric, iun, singleLongLat,ntraj,trajx,trajy, npemp, pemps, metric2, hasCart)

      use pempContents

      real x(*),y(*),values(*)

      real trajx(*),trajy(*)

      double precision xtmp(npts),ytmp(npts)

      double precision trajxtmp(npts),trajytmp(npts)

      double precision singleLongLat(5)

How is it that the declaration of trajxtmps(npts) works?  I would have thought that even if the compiler didn't through an error compiling it, it would blow at run time.

If this is fine now, please let me know.  Thank you.

Sincerely,

CHobbs

0 Kudos
4 Replies
jimdempseyatthecove
Honored Contributor III
392 Views

The subroutine declaration is .NOT. using IMPLICIT NONE, therefore the argument npts is implicitly declared as integer and whatever the value of the integer npts is, declares the size of the arrays using (npts). I am assuming npts is the number of points. For the variables declared with (*) the size is permitted to be unknown but indexible (it is your responsibility to not access beyond bounds of the array).

Added: In the IVF documentation index, search for: assumed-size arrays

Jim Dempsey

 

0 Kudos
Chris_H_3
Beginner
392 Views

Thank you, Jim.  I'm just surprised that the program will have the foresight to allocate the required memory when it gets to that subroutine.  I did not think that was allowed.

0 Kudos
TimP
Honored Contributor III
392 Views
Automatic arrays were standardized in f90. The problem with lack of error checking with implicit allocation could be fixed by changing to allocatable.
0 Kudos
jimdempseyatthecove
Honored Contributor III
392 Views

Note 2,

When npts, in the above example, requires a stack allocation (for local arrays dimensioned with npts), larger than remaining stack, then the call will fail. You can use allocatable or heap arrays to avoid this.

Jim Dempsey

0 Kudos
Reply