title: Understanding the backtrace error in fortran
body: <p>I get the following error when I execute a fortran code compiled with <code>gfortran</code>. The error is followed by a backtrace for this error pointing to memory locations.</p>
  
  <pre><code>Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
  
  Backtrace for this error:
  #0  0x2b2f8e39da2d in ???
  #1  0x2b2f8e39cc63 in ???
  #2  0x311823256f in ???
  #3  0x311827a7be in ???
  #4  0x2b2f8e39cff2 in ???
  #5  0x2b2f8e4adde6 in ???
  #6  0x2b2f8e4ae047 in ???
  #7  0x2b2f8e4a62d7 in ???
  #8  0x40482a in instrument_
      at /home/user/model/instrument.f:90
  #9  0x406c1e in funcdet
      at /home/user/model/funcsynth.f:67
  #10  0x406c98 in main
      at /home/user/model/funcsynth.f:78
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>I would like to know where the first instance of error arises - is it the first line of the backtrace or the lastline? Also, strategies that might help me debug the issue.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Upon backtracing, the line 90 of <code>instrument</code> involves opening a file like so:</p>
  
  <pre><code>  out_file3 = 'new_file'
    OPEN(unit=3,file=out_file3,status='unknown')
  </code></pre>
  
  <p>To determine the issue behind I've incorporated error checking by doing so:</p>
  
  <pre><code>  OPEN(unit=3,file=out_file3,status='unknown',iostat=io_status, err=100)
    100  write(STDOUT,*) 'io status=', io_status 
  </code></pre>
  
  <p>The code exits with the error:
  Error: Invalid value for ERR specification at (1). How do I determine the appropriate value for ERR specification? This led me to suspect that unit=3 might be the cause for error, I've changed the value for unit, but everytime get the "Segmentation fault (core dumped)" error.</p>
  
  <p><strong>Update 2:</strong></p>
  
  <pre><code>OPEN(unit=3,file=out_file3,status='unknown',err=17)
  17  write(STDOUT,*) 'Problem' 
  </code></pre>
  
  <p>Still get the <code>Segmentation fault (core dumped)</code> error at the line corresponding to <code>OPEN...</code>. I can only guess that <code>unit=3</code> is the root cause of the problem.</p>
  
  <p><strong>Update 3</strong></p>
  
  <p>Attempt at a self sufficient example:</p>
  
  <pre><code>  character*280       testfile,finalfile,outfile
    DIR = '/storage/work/user/'
    testfile = 'test.dat'
    CALL getenv(DIR,outfile)
    CALL sappend(outfile,testfile,finalfile)
    OPEN(unit=3,file=finalfine,status='new')
    write(3,*) 'Test'
    END
  </code></pre>