passing assumed-shape arrays in two levels of subroutines (Fortran 90) -


i have had problems calling successive subroutines assumed-shape arrays in fortran 90. more specifically, call 2 levels of subroutines, passing assumed-shape array parameter, in end array lost. demonstrate it, 1 can follow code below.

  program main    interface      subroutine sub1(x)        real, dimension(:):: x        real c      end subroutine sub1       subroutine sub2(x)        real, dimension(:):: x        real c      end subroutine sub2   end interface    real, dimension(:), allocatable:: x    allocate(x(1:10)) ! first executable command in main   x(1) = 5.   call sub1(x)   write(*,*) 'result = ',x(1)   deallocate(x)   end program main    subroutine sub1(x) ! first subroutine   real, dimension(:):: x   real c   call sub2(x)   end subroutine sub1    subroutine sub2(x) ! second subroutine   real, dimension(:):: x   real c   c2=x(1)   end subroutine sub2 

very shortly, main allocates x call sub1(x). sub1 calls sub2(x). means allocated array passed subroutine passes subroutine. expect have in sub2 same array i've created in main, no. using gdb tool explore it, this:

1) in main, before calling sub1, array x defined:

(gdb) p x
$1 = (5, 0, 0, 0, 0, 0, 0, 0, 0, 0)

2) within sub1, before calling sub2, x defined:

(gdb) p x
$2 = (5, 0, 0, 0, 0, 0, 0, 0, 0, 0)

3) inside sub2, however, x have unexpected value , dimension absolutely wrong:

(gdb) p x
$3 = ()
(gdb) whatis x
type = real(4) (0:-1)

so, x has been passed main sub1, not sub1 sub2. i've been using intel fortran gfortran same results.

i've struggling long time. appreciated.
g.oliveira.

the use of assumed-shape dummy arguments requires explicit interface. in main program you've provided explicit interfaces 2 subroutines, these don't propagate subroutines themselves. subroutines compiled separate units, if you've put code 1 source file.
means sub1 doesn't have explicit interface available sub2, , uses implicit interface, argument x assumed real scalar.

all avoided putting 2 subroutines in module , use module in main program, automatically making explicit interfaces available. way don't have provide interfaces yourself, error prone.

as side note, advise use of implicit none in code.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -