Re: Allocating arrays inside a subroutine.



Timothy Hume wrote:

Hi,

I'm wondering if someone could point me in the right direction with this
problem. I'm a new Fortran 90/95 user, but am familiar with 77.

I want to create a Fortran 90 subroutine that reads data from a file, and
returns it in an array, which is one of the arguments to the subroutine...
[snip]
I'm sure this must be a common problem?

Yup. There are two ways.

1.) Use a POINTER argument for your data. This is standard Fortran-90:

subroutine read_data (filename, data_array)
character(*), intent(in) :: filename
real, pointer :: data_array(:)
:
read (unit) nrecords ! header record...
allocate (data_array(nrecords))
! read rest of file
end subroutine

Note that the interface to this subroutine must be explicit to the caller.
So it is recommended that it resides in a module that gets USEd by its caller.

Keep in mind that with POINTERs, memory leakage is a possibility.

2.) As others have noted, if your compiler supports the F2003 ALLOCATABLE array
capability, you can rewrite the above as:

subroutine read_data (filename, data_array)
character(*), intent(in) :: filename
real, allocatable :: data_array(:)
:
read (unit) nrecords ! header record...
allocate (data_array(nrecords))
! read rest of file
end subroutine

Again an explicit interface is required. But with ALLOCATABLE arrays one
does not have to worry about memory leakage.

Last, unrelated to your question, but...

SUBROUTINE READ_DATA(FILENAME, DATA_ARRAY)
CHARACTER(LEN=128), INTENT(IN) :: FILENAME

Note that the above is almost always a bug. That is, unless every one of your
file names is *exactly* 128 characters long. I suspect you meant CHARACTER(LEN=*)
instead.

HTH,

Walt
.



Relevant Pages

  • Re: how to get an allocatable array outside of a subroutine
    ... >>get the values of the allocatable array in the main program). ... > to both the subroutine and your main program. ... of these features are not standard Fortran 90/95, ... One notable exception to the foregoing is Salford's FTN95 compiler. ...
    (comp.lang.fortran)
  • Re: Allocating arrays inside a subroutine.
    ... I'm a new Fortran 90/95 user, ... which is one of the arguments to the subroutine. ... once I've opened up FILENAME and read some header records. ... (here you allocate DATA_ARRAY once you read the necessary headers ...
    (comp.lang.fortran)
  • Re: Allocating arrays inside a subroutine.
    ... Timothy Hume wrote: ... I'm a new Fortran 90/95 user, ... which is one of the arguments to the subroutine. ... Minor point -- I suggest declaring FILENAME as ...
    (comp.lang.fortran)
  • Re: beginner with mex
    ... I have a code which reads a file in fortran and creates a 4 ... The file is in mixed format and I dont ... mex subroutine and call it from matlab. ... you're going to hardwire the filename into the Fortran routine? ...
    (comp.soft-sys.matlab)
  • Re: Fortran based MEX w/ COMMON/SAVE
    ... I believe dat (pointer to the array) and the ... SAVE at the begining of each subroutine is a bit unusual to me. ... > to unload it if there isn't a Fortran END or STOP statement. ... interaction w/ Matlab). ...
    (comp.soft-sys.matlab)