title: Program received signal SIGSEGV: Segmentation fault - invalid memory reference. Backtrace for this error:
body: <p>I have compiled a fortran 95 program in Ubuntu 14.04. Upon running, I got the following error:
  This is different from:<a href="https://stackoverflow.com/questions/23130045/fortran-90-segmentation-fault-invalid-memory-reference-with-scalable-3d-ar">(1)</a></p>
  
  <pre><code>Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
  
  Backtrace for this error:
  ^Z
  [9]+  Stopped                 ./a.out
  </code></pre>
  
  <p>I even tried</p>
  
  <blockquote>
    <p>gfortran -g -fcheck=all -Wall DoubExchange.f95</p>
  </blockquote>
  
  <p>But got the same fault message.</p>
  
  <p>My program:</p>
  
  <pre class="lang-fortran prettyprint-override"><code>! HAMILTONIAN Diagonalization for a 2D Lattice of N sites or (N_X) X (N_Y) size, with DOUBLE EXCHANGE.
  !                                                       !
              !           D O U B L E    E X C H A N G E                   !
  !                                                       !
   PROGRAM DOUBLE_EXCHANGE
     implicit none
     integer:: I, J, K, M, Z, P, L, SIZ, CONT, POINT, S
     integer,parameter :: seed = 8645642
  !   character:: N, U
     real*8:: H
     real :: start, finish
  !     .. Parameters ..
        INTEGER          N, N_X, N_Y
        INTEGER          LDA
        INTEGER          LWMAX
        PARAMETER        ( LWMAX = 1000 )
  !
  !     .. Local Scalars ..
  !
        INTEGER          INFO, LWORK
  !
  !     .. Local Arrays ..
  !     RWORK dimension should be at least MAX(1,3*N-2)
  !
        DOUBLE PRECISION,ALLOCATABLE,DIMENSION(:):: EIG
        REAL*8,ALLOCATABLE,DIMENSION(:,:):: F1
        REAL*8,ALLOCATABLE,DIMENSION(:):: WORK
          call cpu_time(start)
          open(1,file='DOUBLE_EXCHANGE.dat')
          open(2,file='DOUBLE_EXCHANGE.txt')
  !
  !   We are considering a 2D square lattice of 3 X 3
  !
      CALL srand(seed)
      !!  LATTICE DESCRIPTION  !!
      N_X = 2
      N_Y = 2
      SIZ = 2 * N_X * N_Y
      N = SIZ*SIZ
      LDA = N
      !   PROGRAM STARTING POINT  !
      ALLOCATE(F1( SIZ, SIZ ), EIG(SIZ), WORK(3*siz-1))
      CONT = 1
      S = (N_X*2)-1
      M = N_Y-1
  DO P=1,1
      DO I=0,M
         DO J=0,S
           POINT = 1
            DO K=0,M
              DO L=0,S
                  IF(I==K .AND. ABS(J-L)==2) THEN
                    Z = -1
  
                  ELSE IF(J==L .AND. ABS(I-K)==1) THEN
                    Z = -1
  
                  ELSE IF(J==L .AND. ABS(I-K)==M) THEN
                    Z = -1
  
                  ELSE IF(I==K .AND. ABS(J-L)==S-1) THEN
                    Z = -1
  
                  ELSE IF(I==K .AND. J==L) THEN
                    Z = (-1)**POINT
  
                  ELSE
                    Z = 0                                 !       Potential     0
                  ENDIF
              F1(CONT,POINT) = Z
                  POINT = POINT + 1
              ENDDO
           ENDDO
              CONT = CONT + 1
         ENDDO
      ENDDO
  
      WRITE(1,*)
  !
         WRITE(1,*),'Displaying Matrix for the given Hamiltonian'
  !
         DO i=1,SIZ
              WRITE(1,'(18g15.1)') (F1(i,j), j=1,SIZ)
         ENDDO
  !
  !
  !       DSYEV Example Program Results
  !
  !   ALLOCATE(WORK(SIZ))
         WRITE(1,*)
         LWORK = -1
         CALL DSYEV( 'Vectors', 'Upper', SIZ, F1, LDA, Eig, WORK, LWORK, INFO )
  
         LWORK = INT( WORK( 1 ) )
  
  !   DEALLOCATE(WORK)
  
  !   ALLOCATE(WORK(LWORK))
  !
  !   Solve eigenproblem.
  !
         CALL DSYEV( 'Vectors', 'Upper', SIZ, F1, LDA, Eig, WORK, LWORK, INFO )
  !
  !   Checking for convergence.
  !
         IF( INFO.GT.0 ) THEN
            WRITE(*,*)'The algorithm failed to compute eigenvalues.'
            STOP
         END IF
  !
  !
  !       Computes all eigenvalues and, optionally, eigenvectors of an
  !       n x n real symmetric matrix A. The eigenvector v(j) of A satisfies
  !
  !             A*v_i(j) = λ(j)*v_i(j)
  !
  !
          !!   Print eigenvalues. !!
  !
          write(1,*)
          write(1,*)'Eigenvalues'
          write(1,*)
              write(1,'(100g15.5)') (Eig(j), j=1,SIZ)
  !
          !!     Print eigenvectors. Where ψ_(i)(eqv. F1(i,j)), i-label is for energy and j-label is for site.   !!
  !
          write(1,*)
          write(1,*)'Orthonormal Eigenvectors (stored columnwise)'
          write(1,*)
          do i=1,SIZ
              write(1,'(100g15.5)') (F1(i,j), j=1,SIZ)
          end do
          write(1,*)
  
  !
  !        Calculation of number operator, ρ(i) at i-th site for n number of electrons
  !
          WRITE(1,*)'Calculation of number operator, ρ(i) at i-th site'
  !
  !            Considering 3 electrons in the problem
  !
      k = INT(SIZ/2)
         CONT = 1
         L    = 1
      DO I=1,SIZ
         H = 0D0
          DO J=1,K
             H = H + F1(I,J)*F1(I,J)     !  WE ARE CALCULATING Σ_n|ψ_n(i)|^2, Where ψ_n(i) are eigenvectors of i-th site with n-component.
          ENDDO
            IF(MOD(I,S) .NE. 0) THEN
               WRITE(2,'(100g15.5)')L,CONT,H
               CONT =CONT + 1
  
            ELSE
               WRITE(2,'(100g15.5)')L,CONT,H
               WRITE(2,*)
               CONT = 1
               L    = L + 1
            END IF
      ENDDO
  
          WRITE(1,*)
          WRITE(1,*)
          WRITE(1,*)
  ENDDO
  ! 900    format (F8.3,F8.3)
            call cpu_time(finish)
                print '("Time = ",f6.3," seconds.")',finish-start
                write(1, '("Time = ",f6.3," seconds.")'),finish-start
         STOP
  END PROGRAM
  </code></pre>
  
  <p>So, error is mostly about, correct initialization of WORK.</p>