(j3.2006) implicit deallocation in main programs

Tobias Burnus burnus
Tue Sep 13 03:13:14 EDT 2011

On 09/13/2011 08:38 AM, Robert Corbett wrote:
> Oracle Solaris Studio (OSS) Fortran does implicitly
> deallocate unsaved allocatable variables in the main
> program if execution reaches the END statement of
> the main program.

(For what it is worth: gfortran does the same as Oracle's OSS.)

At least in Fortran 2008, the variable has implicitly the SAVE attribute:

"A variable, common block, or procedure pointer declared in the scoping 
unit of a main program, module, or submodule implicitly has the SAVE 
attribute," (5.3.16 SAVE attribute)

By contrast, Fortran 2003 had:

"A SAVE statement may appear in the specification part of a main program 
and has no effect."

> I think our implementation is standard conforming, because it is 
> impossible to tell
> from within a program that the variable has been deallocated.

Glancing at the standard, freeing in main seems as to be valid in 
Fortran 2003 and invalid in Fortran 2008.

Actually, I wonder how one can detect this - and how the your bug 
reported has detected it. The only possibility I see are destructor 
functions [GCC: __attribute__((destructor))] which are run after the 
main program has returned to its caller (e.g. __libc_start_main or 
_start). If the allocatable is a TARGET, the allocatable might be still 
reached by the destructor function.

> Note that we have not yet implemented finalizers, so we have not yet 
> made the mistake of running the finalizers when the END statement is 
> reached.

That's actually something which will surprise users: That reaching the 
end of the main program, the finalization procedures are not run as the 
variables have gained implicitly the save attribute.

Glancing at the F2003 and F2008 standard, I am also not sure whether 
that has changed between F2003 and F2008 or whether I have simply missed 
the right spot.


More information about the J3 mailing list