Nasser M. Abbasi wrote:
> I was wondering if one can give a link or simple answer to this:
> In new Fortran (F90/95/etc...), one can allocate memory.
> Is one required to deallocate it?
If you are using pointers, you have to call deallocate to free them,
they won't get deallocated automatically. (Well, the operating system
frees them after the program has stopped.)
For allocatables: They are automatically freed at the end of the
subroutine/function in which they are declared. However, if you do not
need them any more, deallocating them before the end of the procedure
makes sense. Also, if you want to re-allocate them (without using
move_alloc), you have to deallocate the variable before you can allocate
it again.
In addition, all allocatable variables with the SAVE attribute are not
automatically deallocated. Thus, if you use, e.g., an allocatable module
variable, it makes sense to deallocate it if you do not need the memory
anymore. Also module variables, variables in the main program and other
variables with the SAVE attribute are not finalized. Thus, if you want
to invoke a user-defined finalizer for them, you have to explicitly
deallocate the variable.
In summary: If you only have allocatables and either no big arrays or if
you need the big arrays until the end of the program, you do not need to
call deallocate. If you run into memory issues and have big arrays,
deallocating them explicitly can reduce the required memory.
> I know I need to read more about this.
> Here is an example I was looking at
>
http://rosettacode.org/wiki/Create_a_two-dimensional_array_at_runtime#Fortran
>
> -------------------------------------------------------
> PROGRAM Example
>
> IMPLICIT NONE
> INTEGER :: rows, columns, errcheck
> INTEGER, ALLOCATABLE :: array(:,:)
>
> WRITE(*,*) "Enter number of rows"
> READ(*,*) rows
> WRITE(*,*) "Enter number of columns"
> READ(*,*) columns
>
> ALLOCATE (array(rows,columns), STAT=errcheck) ! STAT is optional
> array(1,1) = 42
>
> WRITE(*,*) array(1,1)
>
> DEALLOCATE (array, STAT=errcheck)
>
> END PROGRAM Example
> -------------------------------------
In this example, the deallocate does not really matter.
At least using Fortran 2008, "array" has implicitly the SAVE attribute
as "array" is declared in the main program. Hence, it will *not* be
finalized automatically (whether it will be automatically deallocated,
depends on the compiler).
But as the deallocate is just before the the program ends, wasting the
memory does not harm as the operating system will take care of freeing
all memory of the program.
However, in order to prevent "valgrind" leak messages or similar
diagnostic, you have to explicitly deallocate allocatable variables
declared in the main program, in the declaration part of modules or
other SAVEd variables.
> What are the rules of thumb to follow on the subject of
> memory allocation/deallocation?
POINTER: It's your responsibility to deallocate them.
ALLOCATABLE: If you leave the scope of the variable (i.e. when you
cannot access them anymore via their name), they are automatically
deallocated. Exception: The have the save attribute (i.e. explicit SAVE,
module variables, variables in the main program).
Tobias