title: passing assumed-shape arrays in two levels of subroutines (Fortran 90)
body: <p>I have had problems calling successive subroutines with assumed-shape arrays in Fortran 90. More specifically, I call two levels of subroutines, passing an assumed-shape array as a parameter, but in the end the array is lost. To demonstrate it, one can follow the code below.</p>
  
  <pre><code>  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) ! The first subroutine
    real, dimension(:):: x
    real C
    call sub2(x)
    end subroutine sub1
  
    subroutine sub2(x) ! The second subroutine
    real, dimension(:):: x
    real C
    C2=x(1)
    end subroutine sub2
  </code></pre>
  
  <p>Very shortly, main allocates x then call sub1(x). Then sub1 calls sub2(x). That means an allocated array is passed to a subroutine that passes it to another subroutine. I would expect to have in sub2 the same array that I've created in main, but no. Using gdb as a tool to explore it, I get this:</p>
  
  <p>1) In main, just before calling sub1, the array x is perfectly defined:</p>
  
  <p>(gdb) p x<br>
  $1 = (5, 0, 0, 0, 0, 0, 0, 0, 0, 0)</p>
  
  <p>2) Within sub1, just before calling sub2, x is also well defined:</p>
  
  <p>(gdb) p x<br>
  $2 = (5, 0, 0, 0, 0, 0, 0, 0, 0, 0)</p>
  
  <p>3) Inside sub2, however, x have an unexpected value and even its dimension is absolutely wrong:</p>
  
  <p>(gdb) p x<br>
  $3 = ()<br>
  (gdb) whatis x<br>
  type = REAL(4) (0:-1)</p>
  
  <p>So, x has been successfully passed from main to sub1, but not from sub1 to sub2. I've been using Intel Fortran an gfortran with the same results.</p>
  
  <p>I've struggling with that for a long time. Any help would be much appreciated.<br>
  G.Oliveira.</p>