Array allocation in Fortran routine

I need to read a lot of data from a file in a Fortran program. The size of the data is variable, so I would like to dynamically allocate arrays. My idea is to make a routine that reads all the data and allocates memory. Simplified version of the program:

program main implicit none real*8, dimension(:,:), allocatable :: v integer*4 n !This subroutine will read all the data and allocate the memory call Memory(v,n) !From here the program will have other subroutines to make calculations end subroutine Memory(v,n) implicit none real*8, dimension(:,:), allocatable :: v integer*4 n,i n=5 allocate(v(n,2)) do i=1,n v(i,1)=1.0 v(i,2)=2.0 enddo return end subroutine Memory 

This program gives me the following error:

 Error: Dummy argument 'v' of procedure 'memory' at (1) has an attribute that requieres an explicit interface for this procedure 

Is this the right way to structure these kinds of programs? If so, how can I solve this error?

Thanks.

+4
source share
1 answer

Assuming you have one source file containing both the program and the subroutine as your message suggests, the easiest solution is to replace the line containing the statement

 end 

with line containing operator

 contains 

and writing at the end of the source file a line containing the statement

 end program 

(Yes, the program keyword is not required, but it is useful.)

The problem your compiler has reported is that since you structured your code, the program knows nothing about the interface for the memory routine, this interface is implicit in Fortran terms. When you want to call a subroutine and either pass or pass a allocated array, the subroutine must have an explicit interface.

There are several ways to provide an explicit interface. One of them, as I showed you, should contain a subroutine inside the program. Another, and more useful way, when your programs get a little big, is to write your routines in modules and use-associate them in a program that wants to use them. Read parts of your Fortran tutorial that describes module and use .

There is at least one more option, but it, especially for beginners, is not attractive, and I will not mention it here.

And while I'm writing, find out and use the intent keyword to indicate whether the argument for the subroutine, written or both, will be considered. This is great help for secure programming, your favorite Fortran resources will be explained in detail.

+8
source

Source: https://habr.com/ru/post/1494933/


All Articles