(j3.2006) implicit deallocation in main programs

Robert Corbett robert.corbett
Tue Sep 13 03:21:18 EDT 2011

On 09/13/11 00:13, Tobias Burnus wrote:
> 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.
The people who reported the bug are developing a tool for
detecting memory leaks.  Their tool would have detected a
leak in their sample program if we had not done the

Bob Corbett

More information about the J3 mailing list