[RFC, Fortran, (pr66775)] Allocatable function result

Steve Kargl sgk@troutmask.apl.washington.edu
Fri Jul 10 18:57:00 GMT 2015


On Fri, Jul 10, 2015 at 06:20:47PM +0200, Mikael Morin wrote:
> 
> I'm not completely convinced by the standard excerpts that have been
> quoted about this topic, as they don't have any explicit mention of
> allocatable variables/expressions.

I did not quote 12.3.3 about "characteristics of function results",
which mentions the allocatable attribute.  But, that is not 
necessarily relevant.  The pieces I quoted explicitly states

   "On completion of execution of the function, the value returned
    is that of its function result. ... If the function result is
    not a pointer, its value shall be defined by the function."

The function not only needs to allocate memory, it needs to
assign it a value.  In the following, if i <= 0, the function
result is not defined. 

module foo
   contains
   function bar(i)
      integer, allocatable :: bar
      integer, intent(in) :: i
      if (i > 0) bar = i
   end function bar
end module foo

program test
   use foo
   integer j
   j = bar( 3); print *, j
   j = bar(-3); print *, j
   end if
end program test

Even if Andre developed a patch to allocate memory in
bar() for the i <= 0 case to prevent the segfault, the
function must return a value.  What should that value be?

I suppose one could argue that gfortran should issue
a run-time error if it can detect the undefined function
result.  But may lead to a run-time penalty.

-- 
Steve



More information about the Gcc-patches mailing list