title: Dynamic arrays allocation in Fortran90
body: <p>I would like to understand the logic behind what I am going to show you.<br>
  If I have a 'module.f90':</p>
  
  <pre><code>module def_dimens
  integer :: dimens=4
  end module def_dimens
  </code></pre>
  
  <p>a 'subr.f90':</p>
  
  <pre><code>subroutine sub(X)
  integer :: X
  integer :: S(1:X*5)
  S=1
  print*,S
  end subroutine sub
  </code></pre>
  
  <p>and a 'main.f90':</p>
  
  <pre><code>program test
  use def_dimens
  call sub(dimens)
  end program test
  </code></pre>
  
  <p>by compiling <code>gfortran module.f90 subr.f90 main.f90</code> and running the result there isn't any problem.</p>
  
  <p>But with 'main2.f90' given by</p>
  
  <pre><code>program test
  use def_dimens
  integer :: A(1:dimens*5)
  A=1
  print*,A
  end program test
  </code></pre>
  
  <p>and compiling <code>gfortran module.f90 main2.f90</code> I have an error, so I have to use an allocatable array:</p>
  
  <pre><code>program test
  use def_dimens
  integer,allocatable :: A(:)
  
  allocate(A(1:dimens*5))
  A=1
  print*,A
  end program test
  </code></pre>
  
  <p>or I have to specify 'dimens' in the module as a parameter (but this is not useful for me,
  because in cases more complicated than this I would need a variable whose value is fixed by calling another subroutine before using it).</p>
  
  <p>So my question is: Why is there such a difference? Why does gfortran complain when it
  has to declare an array in the main program by using a variable to fix its size, and instead there isn't any problem in doing that in a subroutine?</p>