Currently, allocated memory is freed by calling the library function _gfortran_internal_free, which does nothing but:
/* Free internally allocated memory. Pointer is NULLified. Also used to free user allocated memory. */ void internal_free (void *mem) { if (mem != NULL) free (mem); } Two things are worth noting: + although the comment says otherwise, the pointer is not NULLified. + this code could be generated directly by the front-end, leading to better optimization. An example of the missed optimization is the compilation of the following code: REAL :: a(5), b INTEGER :: l, u l = 4 u = 2 a = (/ 1.0, 2.0, 3.0, 4.0, 5.0 /) b = f(a(l:u) - 3.0) call foo(b) CONTAINS REAL FUNCTION f(x) REAL, DIMENSION(:), INTENT(in) :: x f = sum(x) end function END PROGRAM the optimized tree looks like: SR.33 = _gfortran_internal_malloc (0); b = 0.0; _gfortran_internal_free (SR.33); We sure could optimize away the use of SR.33. PR 30720 has another case where such an optimization could happen. -- Summary: Freeing memory doesn't need to call a library function Product: gcc Version: 4.3.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: fortran AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: fxcoudert at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30723