0

Say I have the below code:

program test
  call foo
  call foo
contains
  subroutine foo
    integer(8),dimension(:),allocatable:: var1
    allocate(var1(10))
    ...
    return
  end subroutine foo
end

will the variable var1 get allocated twice? (I guess YES). If it is allocated for each call, will the memory allocated during the first call becomes free?

arunmoezhi
  • 3,082
  • 6
  • 35
  • 54

1 Answers1

7

var1 will (attempt to) be allocated every time the ALLOCATE statement is executed (i.e. every time the foo procedure is called).

Under the rules of Fortran 90 (only) the allocation status of foo becomes undefined when the procedure ends. A variable with undefined allocation status is rendered unusable - you cannot legally re-allocate in a subsequent call of the procedure.

In Fortran 95 and later, because it is a local, non-saved variable, var1 will be deallocated every time execution of the foo procedure ends.

IanH
  • 21,026
  • 2
  • 37
  • 59
  • 2
    The automatic deallocation of local, non-saved allocatables upon exit of procedures was added in Fortran 95 to avoid memory leaks. – M. S. B. Aug 07 '12 at 01:53
  • fortran95? I'm using fortran90. So does the behavior change? – arunmoezhi Aug 07 '12 at 02:06
  • Its not guaranteed in Fortran 90. What compiler is Fortran 90 but not Fortran 95 compliant? – M. S. B. Aug 07 '12 at 03:36
  • If in double (Fortran 90), you can add explicit deallocate statements at the end of procedures. – M. S. B. Aug 07 '12 at 05:22
  • True - I missed the tag for the Fortran 90 standard. – IanH Aug 07 '12 at 10:17
  • @M.S.B. can you please point me to some references where I can read more about this difference w.r.t allocation – arunmoezhi Aug 08 '12 at 00:57
  • If I don't deallocate `var1` in foo and i call foo n number of times. Will the memory footprint increase for each call as `var1` is not deallocated? – arunmoezhi Aug 08 '12 at 01:00
  • If you are using a compiler that supports Fortran 95 (which is **all** currently supported Fortran compilers that I am personally aware of) - No. – IanH Aug 08 '12 at 03:38
  • i'm using intel fortran compiler and the code has an extension .f90. – arunmoezhi Aug 08 '12 at 06:25
  • With that compiler (with most Fortran compilers) the extension of the file is mostly irrelevant with respect to standard level, except that a convention exists that files with a f90 extension are *free* *form* *source* (which was introduced with Fortran 90). The Intel compiler is definitely a Fortran 95 compiler - it is not far off being a Fortran 2003 compiler. Using that compiler the local nonsaved allocatable array var1 will be deallocated when the procedure foo exits. – IanH Aug 08 '12 at 09:33
  • The Intel compiler is _almost_ a Fortran 95 compiler - it doesn't implement some parts of the F95 standard (yet). – Hristo Iliev Aug 08 '12 at 14:08
  • @Hristo There are explicit statements by the vendor that they support Fortran 95. Any non-conformances as such are compiler bugs. I'm not aware of any such bugs that would be relevant to the original question. – IanH Aug 08 '12 at 16:32
  • @IanH, right, I was thinking about F2003 compliance and got them mixed up. (Note to self: no more SO after tiresome seminar activities!) – Hristo Iliev Aug 08 '12 at 16:49