Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26730 Discussions

Declaring array in subroutine based on passed argument for size

Chris_H_3
Beginner
126 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
Black Belt
126 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

 

Chris_H_3
Beginner
126 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.

TimP
Black Belt
126 Views
Automatic arrays were standardized in f90. The problem with lack of error checking with implicit allocation could be fixed by changing to allocatable.
jimdempseyatthecove
Black Belt
126 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

Reply