Fortran allocatable array lifetime
Asked Answered
O

1

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?

Overanxious answered 7/8, 2012 at 0:6 Comment(0)
C
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.

Christiechristin answered 7/8, 2012 at 0:48 Comment(13)
The automatic deallocation of local, non-saved allocatables upon exit of procedures was added in Fortran 95 to avoid memory leaks.Teddie
fortran95? I'm using fortran90. So does the behavior change?Overanxious
Its not guaranteed in Fortran 90. What compiler is Fortran 90 but not Fortran 95 compliant?Teddie
If in double (Fortran 90), you can add explicit deallocate statements at the end of procedures.Teddie
True - I missed the tag for the Fortran 90 standard.Christiechristin
@M.S.B. can you please point me to some references where I can read more about this difference w.r.t allocationOveranxious
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?Overanxious
If you are using a compiler that supports Fortran 95 (which is all currently supported Fortran compilers that I am personally aware of) - No.Christiechristin
i'm using intel fortran compiler and the code has an extension .f90.Overanxious
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.Christiechristin
The Intel compiler is almost a Fortran 95 compiler - it doesn't implement some parts of the F95 standard (yet).Seasickness
@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.Christiechristin
@IanH, right, I was thinking about F2003 compliance and got them mixed up. (Note to self: no more SO after tiresome seminar activities!)Seasickness

© 2022 - 2024 — McMap. All rights reserved.