title: How can I check if a subroutine has been run before?
body: <p>I've currently got a Fortran program using the 2008 standard that has a subroutine that loads data from a file if it's the first run of the subroutine. On all runs, the subroutine interpolates over the data and returns two values, but the goal is to avoid reloading the same data from the file.</p>
  
  <p>Initially, I had something like this:</p>
  
  <pre><code>module myModel_mod
      use myModelLoader_mod
      use linear_interpolation_module
      implicit none
      contains
      subroutine myModel(A, B, C, modelFile, D, E)
          real :: A, B, C, D, E
          character(len=*) :: modelFile
          type(linear_interp_3d), save :: F, G
          real, dimension(:), allocatable, save :: As, Bs, Cs
          if (.not. allocated(As)) then
              call loadModel(modelFile, As, Bs, Cs)
              .
              .  (processing of loaded data and creation of F and G occurs here)
              .
          end if
          call F%evaluate(A, B, C, D)
          call G%evaluate(A, B, C, E)
      end subroutine
  end module
  </code></pre>
  
  <p>My module makes use of the <a href="https://github.com/jacobwilliams/finterp" rel="nofollow noreferrer">finterp</a> library. It interpolates a 3D gridded data set for two values, D and E. I'm having to rewrite this part of the code to fix a memory leak, and I'd like to fix where <code>As</code>, <code>Bs</code>, and <code>Cs</code> are left allocated. They don't need the SAVE attribute; it's left over from an older implementation of gridded interpolation I was using. However, if I remove it, then by my understanding, I can't check if <code>As</code> is allocated to see if the subroutine has been run before.</p>
  
  <p>I've considered creating a logical flag variable with the SAVE attribute that gets set when the subroutine runs for the first time, but I believe that would still result in a small memory leak, albeit much smaller than what I currently have.</p>
  
  <p>Is there a way to check for the "first run" condition in a way that doesn't result in a memory leak?</p>