title: Segmentation error after second user input
body: <p>I compile as such:</p>
  
  <pre><code>f77 -c readheader.F
  f77 -c readheader2.F
  make (for the main program, which is necessary to have a makefile for)
  </code></pre>
  
  <p>and run:</p>
  
  <pre><code>./decpar
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Which datafile?
  Point 2a
  
  Program received signal SIGSEGV: Segmentation fault - invalid memory
  reference.
  
  Backtrace for this error:
  #0  0x7FF5E4A62E08
  #1  0x7FF5E4A61F90
  #2  0x7FF5E41934AF
  #3  0x7FF5E4B25C40
  #4  0x7FF5E4B26197
  #5  0x7FF5E4B34B87
  #6  0x4056B8 in __mysubs2_MOD_readheader2
  #7  0x401CA5 in MAIN__ at decpar.F:29 (discriminator 88)
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>I'm not going to post the whole code because it's huge and unnecessary. Luckily, the error appears in the first few lines. I have two modules in both of which the user is required to type the name of a datafile so that information is extracted and directed to the main program. I tried to flush(5) the datafile in case for some strange reason it's not overwritten by the second user input and I get "Segmentation fault - invalid memory reference.". I tried to change the name of datafile to datafile2 to create new space for the second input, but I get the same problem as when the name is the same. Everything I have tried stops at 'Point 2a'. Also, if I switch the order of the modules call, the problem persists in the second file. After more trying gfortran -c -g -fbacktrace -fcheck=all, the error I got was:
  Fortran runtime error: Index '1' of dimension 1 of array 'thetax' above upper bound of 0. </p>
  
  <p>P.S.: Some commands that are not indented properly are just written as such, for formatting purposes here.</p>
  
  <pre><code>  module MySubs
    contains
  
    subroutine readheader(theta,thetax,thetay,thetaz)
  
    include 'psize.inc'
    include 'pinfo.inc'
    #include "rvarrays.inc"
    include 'itype.inc'
    include 'iodefs.inc'
  
    REAL,ALLOCATABLE, intent(inout), DIMENSION (:) :: thetay, thetaz
    REAL,ALLOCATABLE, intent(inout), DIMENSION (:) :: theta, thetax
  
    ALLOCATE (theta(nobj),thetax(nobj),thetay(nobj),thetaz(nobj))
  
    1    print*,'Which datafile?'
    read(*,'(a)') datafile 
  
    CALL readdata
  
    do i=1,nobj
      if (i.LT.nobj) then
          thetax(i)=(v(1,i+1)-v(1,i))*(r(1,i+1)-r(1,i))**(-1)
          thetay(i)=(v(2,i+1)-v(2,i))*(r(2,i+1)-r(2,i))**(-1)
          thetaz(i)=(v(3,i+1)-v(3,i))*(r(3,i+1)-r(3,i))**(-1)
          theta(i)=thetax(i)+thetay(i)+thetaz(i)
          else 
          thetax(i)=(v(1,i)-v(1,i-1))*(r(1,i)-r(1,i-1))**(-1)
          thetay(i)=(v(2,i)-v(2,i-1))*(r(2,i)-r(2,i-1))**(-1)
          thetaz(i)=(v(3,i)-v(3,i-1))*(r(3,i)-r(3,i-1))**(-1)
          theta(i)=thetax(i)+thetay(i)+thetaz(i)
      endif 
    end do
    RETURN
    end subroutine readheader
  
    end module MySubs
  </code></pre>
  
  <hr>
  
  <pre><code>  module MySubs2
    contains
  
    subroutine readheader2(thetapx, thetapy, thetapz,thetap)
  
    include 'psize.inc'
    include 'pinfo.inc'
    #include "rvarrays.inc"
    include 'itype.inc'
    include 'iodefs.inc'
  
    character*1 answer
    REAL :: z2
    REAL,ALLOCATABLE,intent(inout),DIMENSION (:) :: thetapz,thetap
    REAL,ALLOCATABLE,intent(inout),DIMENSION (:) :: thetapx, thetapy
    ALLOCATE (thetapx(nobj),thetapy(nobj),thetapz(nobj))
  
  
    1    print*,'Which datafile?'
    print*, 'Point 2a'
    read(*,'(a)') datafile
    print*, 'Point 2b'
    CALL readdata
  
   do i=1,nobj
  
    if (i&lt;nobj) then
      thetapx(i)=(v(1,i+1)-v(1,i))*(r(1,i+1)-r(1,i))**(-1)
      thetapy(i)=(v(2,i+1)-v(2,i))*(r(2,i+1)-r(2,i))**(-1)
          thetapz(i)=(v(3,i+1)-v(3,i))*(r(3,i+1)-r(3,i))**(-1)
          thetap(i)=thetapx(i)+thetapy(i)+thetapz(i)
    else 
      thetapx(i)=(v(1,i)-v(1,i-1))*(r(1,i)-r(1,i-1))**(-1)
      thetapy(i)=(v(2,i)-v(2,i-1))*(r(2,i)-r(2,i-1))**(-1)
          thetapz(i)=(v(3,i)-v(3,i-1))*(r(3,i)-r(3,i-1))**(-1)
          thetap(i)=thetapx(i)+thetapy(i)+thetapz(i)
    endif 
  
   end do
  
    RETURN
    end subroutine readheader2
  
    end module MySubs2
  </code></pre>
  
  <hr>
  
  <pre><code>  PROGRAM decpar
  
    use MySubs
    use MySubs2
  
    include 'psize.inc'
    include 'pinfo.inc'
    #include "rvarrays.inc"
    include 'itype.inc'
    include 'iodefs.inc'
  
    REAL,ALLOCATABLE,DIMENSION(:) :: dztheta,dthetax,dthetay,dthetaz
    REAL,ALLOCATABLE,DIMENSION(:) :: convtheta,dttheta,q1,q2,q3,q
    REAL,ALLOCATABLE,DIMENSION(:) :: theta,thetax,thetay,thetaz
    REAL,ALLOCATABLE,DIMENSION(:) :: thetapx, thetapy, thetapz,thetap
    REAL,ALLOCATABLE,DIMENSION(:) :: dthetasp
  
    ALLOCATE(dztheta(nobj),dthetax(nobj),dthetay(nobj),dthetaz(nobj))
    ALLOCATE(q(nobj),dthetasp(nobj),q3(nobj))
    ALLOCATE(convtheta(nobj),dttheta(nobj),q1(nobj),q2(nobj))
  
    CALL readheader(theta,thetax,thetay,thetaz)
    CALL readheader2(thetapx, thetapy, thetapz,thetap)
  
    q0=-0.55
    do i=1,nobj
      dztheta(i)=(thetap(i)-theta(i))*(z1-z2)**(-1)
      dttheta(i)=-(2./3.)*dztheta(i)
      if (i&lt;nobj) then
         dthetax(i)=r(1,i)*(thetax(i+1)-thetax(i))/(r(1,i+1)-r(1,i))
         dthetay(i)=r(2,i)*(thetay(i+1)-thetay(i))/(r(2,i+1)-r(2,i))
         dthetaz(i)=r(3,i)*(thetaz(i+1)-thetaz(i))/(r(3,i+1)-r(3,i))
         dthetasp(i)=dthetax(i)+dthetay(i)+dthetaz(i)
      else 
         dthetax(i)=r(1,i)*(thetax(i)-thetax(i-1))/(r(1,i)-r(1,i-1))
         dthetay(i)=r(2,i)*(thetay(i)-thetay(i-1))/(r(2,i)-r(2,i-1))
         dthetaz(i)=r(3,i)*(thetaz(i)-thetaz(i-1))/(r(3,i)-r(3,i-1))
         dthetasp(i)=dthetax(i)+dthetay(i)+dthetaz(i)
      endif
    convtheta(i)=dttheta(i)+h100*100.0*dthetasp(i)  
    q1(i)=-1+(1+q0)*(1+theta(i)/(3.0*100.0*h100))**(-2)
    q2(i)=-(dttheta(i)/(3*100*h100)**2)
    q3(i)=(1+theta(i)/(3*100*h100))**(-2)
    q(i)=q1(i)+q2(i)*q3(i)
    end do
  
    DEALLOCATE(dztheta,dthetax,dthetay,dthetaz)
    DEALLOCATE(q,q1,q2,q3,dthetasp)
    DEALLOCATE(convtheta,dttheta)
  
    end
  </code></pre>