This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug fortran/46487] allocatable scalars leak memory (allocatable_scalar_5.f90)
- From: "burnus at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 16 Nov 2010 13:50:04 +0000
- Subject: [Bug fortran/46487] allocatable scalars leak memory (allocatable_scalar_5.f90)
- Auto-submitted: auto-generated
- References: <bug-46487-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46487
--- Comment #1 from Tobias Burnus <burnus at gcc dot gnu.org> 2010-11-16 13:49:58 UTC ---
Well, the problem is actually simple:
program test
implicit none
integer :: b
b = func()
contains
function func ()
integer, allocatable :: func
allocate(func)
func = 5332
end function func
end program test
The function "func" returns the allocatable scalar as:
return __result_func;
In the main program, one has:
b = *func ();
or - if "b" is allocatable:
*b = *func ()
However, the proper method should be:
{
tmp = func()
b = *tmp
free (tmp);
}
(Or replace: "free (tmp)" by "if (tmp != NULL) free(tmp)", which is identical
as free also accepts NULL.)
For arrays, it currently works without this detour as there the argument is
passed by value; doing likewise for allocatable scalars would be one -- ABI
breaking -- alternative.