From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8144 invoked by alias); 4 Nov 2012 18:27:09 -0000 Received: (qmail 7988 invoked by uid 48); 4 Nov 2012 18:26:50 -0000 From: "janus at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug fortran/55207] New: Automatic deallocation of variables declared in the main program Date: Sun, 04 Nov 2012 18:27:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: janus at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-11/txt/msg00296.txt.bz2 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: unassigned@gcc.gnu.org ReportedBy: janus@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).