title: deallocation and memory allocation problems in FORTRAN
body: <p>I am having problems with the deallocate and allocate aspects of part of my  FORTRAN code.  in particular, i think that the issue has to do with memory allocation from a search on my error message on the web. The error message talks about invalid pointers, however, I am not using any pointers in my program</p>
  
  <p>After completing iteration # 2 of my f loop (see below), the program crashes or rather most of the time it crashes and sometimes it just freezes up. I am confident that this is the point where the bug is. as the program runs up to this point. </p>
  
  <p>I have subroutines not shown but since they work for other simulation combinations, I am reasonably confident that they are not the problem. I am using deallocate and allocate in other places within the program (successfully) so I am surprised that it is not working here.</p>
  
  <p>I am only showing part of the program for ease of reading. in particular, I have removed my calls to the subroutines that I wrote. I hope that i have provided sufficient info for you programmers to help me figure out the problem. if not please specify what other info you want and I will be happy to comply. I have compiled the program using various compiler options and have fixed some bugs and removed any warnings. At this point, however, the compiler options do not give me any more info.</p>
  
  <pre><code>allocate(poffvect(1:6))
  allocate(phi1out(1:1))
  allocate(phi2out(1:1))
  allocate(phi1outs1(1:1))
  allocate(phi2outs1(1:1))
  
       dummy allocation
     allocate(phi1outind(1:1))
     allocate(phi2outind(1:1))
     allocate(phi1outinds1(1:1))
     allocate(phi2outinds1(1:1))
  
     do e = 1, 6
       print *,"e", e
       do f = 1, 3
  
         print *,"f", f, iteratst1(f), trim(filenumcharimp)
  
         deallocate(phi1outinds1, STAT = AllocateStatus)
       if (AllocateStatus /= 0) stop "Error during  deallocation of phi1outinds1"
       print *, "Allocatestatus of phi1outinds1 is",  AllocateStatus
       deallocate(phi2outinds1, STAT = AllocateStatus)
      print *, "DeAllocatestatus of phi1outinds2 is", AllocateStatus
  
       if (AllocateStatus /= 0) stop "Error during deallocation of phi2outinds1"
      print *, "we deallocate f loop ok", iteratst1(f)
  
        allocate(phi1outinds1(1:iteratst1(f)), STAT = AllocateStatus)
       if (AllocateStatus /= 0) stop "Error during allocation of phi1outinds1"
       allocate(phi2outinds1(1:iteratst1(f)), STAT = AllocateStatus)
      if (AllocateStatus /= 0) stop "Error during deallocation of phi1outinds1"
  
   end do
   end do
  </code></pre>
  
  <p>compiler options</p>
  
  <pre><code>ifort -free -check  -traceback -o adatptmultistage1new.out adatptmultistage1new.f90
  </code></pre>
  
  <p>output</p>
  
  <pre><code>       e           1
         f           1        5000 43
       DeAllocatestatus of phi1outinds1 is           0
      DeAllocatestatus of phi1outinds2 is           0
      we deallocate f loop ok        5000
     f loop done           1
    f           2       10000 43
   Allocatestatus of phi1outinds1 is           0
   DeAllocatestatus of phi1outinds2 is           0
   we deallocate f loop ok       10000
     f loop done           2
     f           3       15000 43
    Allocatestatus of phi1outinds1 is           0
  </code></pre>
  
  <p>error message</p>
  
  <pre><code>*** glibc detected *** ./adatptmultistage1new.out: munmap_chunk(): invalid pointer:   0x0000000000d3ddd0 ***
      ======= Backtrace: =========
      /lib/libc.so.6(+0x77806)[0x7f5863b7b806]
     .    /adatptmultistage1new.out[0x43247c]
     .    /adatptmultistage1new.out[0x404368] 
     ./adatptmultistage1new.out[0x4031ec]
     /lib/libc.so.6(__libc_start_main+0xfd)[0x7f5863b22c4d]
    .    /adatptmultistage1new.out[0x4030e9]
     ======= Memory map: ========
  00400000-004d4000 r-xp 00000000 08:03    9642201 
  /home/jgold/smwcv/error_infect/test/surfaces/multistage/adaptonly/adatptmultistage1new.out
  006d4000-006dc000 rw-p 000d4000 08:03 9642201
  </code></pre>
  
  <p>[rest of error message not shown for brevity]</p>
  
  <pre><code>  7fffb004d000-7fffb00bc000 rw-p 00000000 00:00 0                          [stack]
    7fffb01d7000-7fffb01d8000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
    Aborted
  </code></pre>