http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55207
Bug #: 55207 Summary: Automatic deallocation of variables declared in the main program Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: fortran AssignedTo: unassig...@gcc.gnu.org ReportedBy: ja...@gcc.gnu.org Although F95/F03/F08 only require automatic deallocation of local unsaved variables upon termination of a procedure, gfortran currently also does it upon termination of the main program. Simple test case (for both scalars and arrays): program main integer, allocatable :: i integer, allocatable, dimension(:) :: j allocate(i) allocate(j(1:5)) end program The dump shows a block which automatically deallocates the variables at the end of the main program: finally { if (j.data != 0B) { __builtin_free ((void *) j.data); } j.data = 0B; if (i != 0B) { __builtin_free ((void *) i); } } >From F08:6.7.3.2: "When the execution of a procedure is terminated by execution of a RETURN or END statement, an unsaved allocatable local variable of the procedure retains its allocation and definition status if it is a function result variable or a subobject thereof; otherwise, it is deallocated." This mentions only *procedures*, not the main program. Moreover, variables declared in the main program are implicitly SAVEd in F08, cf. chapter 5.3.16: "A variable, common block, or procedure pointer declared in the scoping unit of a main program, module, or submodule implicitly has the SAVE attribute, which may be confirmed by explicit specification. If a common block has the SAVE attribute in any other kind of scoping unit, it shall have the SAVE attribute in every scoping unit that is not of a main program, module, or submodule." Currently we miss to flag this via attr.save = SAVE_IMPLICIT. In F95 it is basically impossible to detect whether a compiler does end-of-program auto-dealloc. However, in F03 it can be detected by using a FINAL procedure, since deallocation (automatic or explicit) also implies finalization (for finalizable variables).