title: Fortran pointer functions: why does this code's behavior depend on the order of function calls?
body: <h1>Context</h1>
  
  <p>The toy Fortran code posted below calls two pointer functions.  That is, both functions return a pointer.  In fact, they're both array pointers.  They both attempt to do the same thing, which is to return an integer array pointer referencing an integer array having three elements, 1, 2, and 3.  The first function uses the pointer assignment operator (=>) to point the function pointer to an allocatable array that holds the data.  The second function allocates a block of dynamic memory directly, via the pointer, for storing the data.  The calling program just prints the elements of the returned array(s).</p>
  
  <p>Here's what I find odd.</p>
  
  <ol>
  <li>If I point <code>a</code> to the result of <code>function1</code>, the results are not correct.  The first element of <code>a</code> appears to be "clobbered":  <code>a</code> has <code>0</code>, <code>2</code>, <code>3</code>.</li>
  <li>If I point <code>b</code> to the result of <code>function2</code>, the results are correct.  <code>b</code> gets <code>1</code>, <code>2</code>, <code>3</code>.</li>
  <li>Stranger still, pointing <code>b</code> to the result of <code>function2</code> <strong><em>after</em></strong> pointing <code>a</code> to <code>function1</code> <strong><em>changes</em></strong> <code>a</code> such that it becomes correct.  <code>a</code> then has <code>1</code>, <code>2</code>, <code>3</code>.</li>
  </ol>
  
  <h1>Question</h1>
  
  <p>Why does this occur?  More precisely, why does a pointer function that returns a pointer to an allocatable array clobber the first element of that array for the caller?  More precisely still, why does pointing one pointer (<code>b</code>) produce a side-effect on another pointer (<code>a</code>), where the targets come from different functions that are written so as not to interact with each other at all?</p>
  
  <h1>Caveats</h1>
  
  <p>I get this behavior using the GNU Fortran compiler v.4.3.3, running an Intel laptop with Ubuntu (Jaunty).  Your results may vary, which might be more interesting still.  Finally, as always it could be operator error on my part, which would be interesting to me at least.</p>
  
  <h1>Code</h1>
  
  <pre><code>program main
    implicit none
    integer, dimension(:), pointer :: a, b
    integer :: i
    a =&gt; function1()
    b =&gt; function2()
    do i = 1, 3
       print *, a(i)
    end do
    ! do i = 1, 3
    !    print *, b(i)
    ! end do
  contains
    function function1 ()
      integer, dimension(:), allocatable, target :: array
      integer, dimension(:), pointer :: function1
      allocate(array(3))
      array(1) = 1
      array(2) = 2
      array(3) = 3
      function1 =&gt; array
    end function function1
  
    function function2 ()
      integer, dimension(:), pointer :: function2
      allocate(function2(3))
      function2(1) = 1
      function2(2) = 2
      function2(3) = 3
    end function function2
  end program main
  </code></pre>