Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

allocatable array to subroutine won't work 1

Status
Not open for further replies.

Bobthebuilder242

Technical User
Sep 4, 2010
3
US
Hi guys, i'm a beginner Fortran programmer and i am having some serious problems passing an allocatable multidimensional array to a subroutine. I have tried several things and all result in a
Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1)
In file perverse.f95:11
and

Allocate(Matrix(1:5,1:5))
1
Error: Syntax error in ALLOCATE statement at (1)

errors. Can you please help.
 
First of all, do not define external subroutines : never !

I advise to put the subroutine createMatrix in a module and to USE that module in the main program :

Code:
module matrix_module
contains

subroutine createMatrix(Matrix)

  real, dimension(:, :), allocatable, Intent(in out ) :: Matrix

  Allocate(Matrix(1:5,1:5))

  print *,"Hi World!"

end subroutine createMatrix
end module



program perverse
  USE matrix_module

  real, dimension(:, :), allocatable :: A
  call createMatrix(A)

end program perverse

The difference with the initial version is that the main program, thanks to the instruction USE, exactly knows what the subroutine createMatrix expects as argument.

By default, the attribute ALLOCATABLE is not passed to an external routine. To achieve that, it is needed to declare an explicit interface which is itself error prone.

So again : avoid external subroutines or functions please !
 
Thanks for your reply FJacq, but when i try using your solutions i get these errors, and from what I've researched online it seems that fortran is not compiling the matrix_module file in time.


In file perverse.f95:10

real, dimension:), :), allocatable, Intent(in out ) :: Matrix
1
Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1)
In file perverse.f95:12

Allocate(Matrix(1:5,1:5))
1
Error: Syntax error in ALLOCATE statement at (1)
In file perverse.f95:22

USE matrix_module
1
Fatal Error: Can't open module file 'matrix_module.mod' for reading at (1): No such file or directory
 
 http://www.mediafire.com/?lbrlr7y1ao2kjtg
I have compiled my solution before posting it. I used the ifort-11 (intel compiler) and also gfortran-4.5 (the Gnu compiler)

In fact, it really depends on your compiler. Using the ALLOCATABLE attribute for a dummy argument is a FORTRAN-2003 feature. That feature is a common extension of FORTRAN-95 compilers because it was the object of a Technical Recommendation (TR15581 in 1998). So most of recent FORTRAN-95 compilers implement it.

But if you have a strict FORTRAN-95 compiler, then you are right : only the POINTER attribute is allowed for a dummy argument.
 
Another comment anyway : put your subroutine in a module because the POINTER attribute is like the ALLOCATABLE attribute : the calling routine needs to know the signature of the called routine.

At last, about the second error message " Fatal Error: Can't open module file 'matrix_module.mod'...", it is simply due to the fact that the compiler has not created the file matrix_module.mod because of the first mistake.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top