title: what is "segmentation fault - invalid memory reference error" in FORTRAN
body: <p>I am receiving the following error in my Fortran code :</p>
  
  <pre><code>  Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
  
    Backtrace for this error:
    #0  0x7F80C7B46777
    #1  0x7F80C7B46D7E
    #2  0x7F80C7282D3F
    #3  0x402D1A in __mymodule_MOD_gcr_mfree
    #4  0x412175 in MAIN__ at HUHSI1.f90:?
    Segmentation fault (core dumped)
    ------------------
    (program exited with code: 139)
  </code></pre>
  
  <ol>
  <li><p>I don't understand this error completely. What is this error?</p>
  
  <p>Number 3 indicates the following subroutine :</p>
  
  <pre><code>SUBROUTINE gcr_Mfree(F2,Cnew,Cold,C_Fold,xm,converged)
  
      !**** FUNCTIONS TO BE SOLVED ****
      ! Generalized Conjugate Residual Algorithm
      ! Solves M*x=b (J*dx=-F)
  
      IMPLICIT REAL*8 (A-H,O-Z)
  
      INTEGER, intent(out)                :: converged  
      REAL*8, DIMENSION(:,:), ALLOCATABLE :: F2,p,Mp             
      REAL*8, DIMENSION(:),   ALLOCATABLE :: F2V,F22V,CnewV,ColdV,C_FoldV,alpha2,r,b,Mr,xv
  
      ! 2D INPUT VARIABLE DIMENSIONS:
      DIMENSION F22(-2:IG+2,-2:JG+2)
      DIMENSION Cnew(-2:IG+2,-2:JG+2)
      DIMENSION Cold(-2:IG+2,-2:JG+2)
      DIMENSION C_Fold(-2:IG+2,-2:JG+2)
      DIMENSION xm(-2:IG+2,-2:JG+2)
  
      integer :: j,maxiter,iter
      real*8 :: tol,normr  
      !===================================================================           
  
      MASK = SIZE(F2)                 ! Size of F2 : (IG+5)*(JG+5)
      Print*, 'MASK IS EQUAL TO',MASK
      ALLOCATE(F2V(1:MASK))
      ALLOCATE(CnewV(1:MASK))
      ALLOCATE(ColdV(1:MASK))
      ALLOCATE(C_FoldV(1:MASK))
      ALLOCATE(xv(1:MASK))
  
      ALLOCATE(r(1:MASK))
      ALLOCATE(b(1:MASK))
      ALLOCATE(p(1:IG,1:JG))
      ALLOCATE(Mp(1:IG,1:JG))
  
      !************* RESHAPING MATRICES TO VECTORS **************
      F2V     = RESHAPE(F2    ,(/MASK/))
      ColdV   = RESHAPE(Cold  ,(/MASK/))
      CnewV   = RESHAPE(Cnew  ,(/MASK/))
      C_FoldV = RESHAPE(C_Fold,(/MASK/))
  
     !            Write(*,*) shape(CnewV), shape(Cnew)
     !            WRITE(*,*) 'Cold='
     !            WRITE(*,554) Cold
     !            WRITE(*,*) 'Cnew='
     !            WRITE(*,554) Cnew
     !554         FORMAT(F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F5.3,1X,F5.3,1X)     
  
      b       = -F2V(1:MASK)                !(why minus?)
  
      tol     = 5.E-2
      alpha2  = 1e-6 
      maxiter = MASK
      r       = b(1:MASK)               ! we need this when calculating r_new
      normr    = sqrt(sum( r(:)**2 ))                  !!!! Norm               
  
      557         FORMAT(1X,F5.2,2X,F5.2,10X,F5.2,4X,F5.2,10X,F5.2,2X,F5.2)
      WRITE(*,*) 'ColdV,CnewV'
      WRITE(*,557) ColdV,CnewV,F2V,C_FoldV,b,r
      PRINT*, 'normr=', nomr
  
      !************* MAIN CALCULATION ****************
      DO iter=1,maxiter
  
          !Get preliminary search direction
          p(:,iter) = r(:)
  
                          WRITE(*,*) 'p='
                          WRITE(*,558) ,p
          558             FORMAT(1X,F4.2) 
  
          ! Approximate the Jacobian(M) residual product
          CnewV = CnewV + alpha2 * r
  
          WRITE(*,*), 'CnewV='
          WRITE(*,559) ,CnewV
          559             FORMAT(1X,F4.2)
  
          Cnew = RESHAPE(CnewV,(/IG+5,JG+5/))                 
  
          !********** Call Crank-Nicolson Function               
  
          F22V= RESHAPE(F22,(/MASK/))
  
          Mr(:) = (1/alpha2)*(F22V-F2V)      !GUIDE: (The apporximated Jacobian matrix)
  
          Mp(:,iter) = Mr(:)
  
          !! Orthogonalize search direction
          do j = 1, iter-1
              p(:,iter)  = p(:,iter)  - sum( Mp(:,j) * Mp(:,iter) ) * p(:,j)
              Mp(:,iter) = Mp(:,iter) - sum( Mp(:,j) * Mp(:,iter) ) * Mp(:,j)
          enddo
  
          !Normalize search direction              
          p(:,iter)  =  p(:,iter) / (norm2(Mp(:,iter))) 
          Mp(:,iter) = Mp(:,iter) / (norm2(Mp(:,iter)))
  
          !Update solution and residual
          alpha2 = sum( r(:) * Mp(:,iter) ) / sum( Mp(:,iter)**2 )                            
          xv = xv + alpha2 * p(:,iter)
          r = r - alpha2 * Mp(:,iter)      ! where is the *(Cnew - C)?
  
          xm = RESHAPE(xv,(/IG+5,JG+5/)) 
  
          !Check convergence
          normr=sqrt(sum( r(:)**2 ))                               !!!! norm
          !fprintf('norm(r) = !g iter = !gNewLine',normr,iter+1);
          if (normr.LT.tol) then
              converged=1
              exit
          end if
  
  
      END DO 
  
      if (normr &gt; tol) then
          write(*,*) 'GCR SOLUTION DID NOT CONVERGE!'
          converged=0
      end if
  
      RETURN
  END subroutine gcr_Mfree
  </code></pre>
  
  <p>Here are a couple of things I have done to figure out why I am receiving this error: </p>
  
  <p>I have traced the values of the variables in my code by printing them step by step. I have reached this formula where I am not able to print the value for <strong>CnewV</strong> anymore.</p>
  
  <pre><code>CnewV = CnewV + alpha2 * r
  </code></pre></li>
  <li><p>This formula is in "mymodule_MOD_gcr_mfree" which is indicated by #3 in the error message. <strong>Why the error is not indicating any row number?</strong></p>
  
  <p>"r" is a vector. "alpha2" is scalar. CnewV at the RHS is a vector. Size and rank of "r" and "CnewV" are the same and the value of only one element in "CnewV" vector is <strong>NaN</strong>. Also, two or three elements are printed "*****" when I am trying to print them at screen.</p></li>
  <li><p>when printing the matrix "p" (the format number is 558), there are much less elements in the array p. There are 221 elements in array "r", however, there are 96 in "p"</p></li>
  </ol>
  
  <p>This is a complicated problem. I hope the given information is enough to understand the error.</p>