------- Comment #2 from sgk at troutmask dot apl dot washington dot edu 2009-11-20 00:20 ------- Subject: Re: overloaded function with allocatable result problem
If the code is compiled with -fdump-tree-original one immediately see the cause of the runtime error. Eliminating the common code in the dumps, I find this code pure function f() result(i) integer :: i integer, allocatable :: loc_ar(:) allocate(loc_ar(1)) loc_ar = gen_g() ! does not work deallocate(loc_ar) i = 1 end function f generates f () { g (&loc_ar); } loc_ar has been allocated and the pointer to it is passed to g() where is becomes associated with the result variable j. You then try to allocate j, which is already allocated. When you call g() directly in pure function f() result(i) integer :: i integer, allocatable :: loc_ar(:) allocate(loc_ar(1)) loc_ar = g() ! no problem here deallocate(loc_ar) i = 1 end function f the dump shows f () { { integer(kind=8) D.1398; struct array1_integer(kind=4) atmp.1; integer(kind=8) D.1393; integer(kind=8) D.1392; integer(kind=8) D.1391; integer(kind=4)[0:] * restrict D.1390; D.1390 = (integer(kind=4)[0:] * restrict) loc_ar.data; D.1391 = loc_ar.offset; D.1392 = loc_ar.dim[0].lbound; D.1393 = loc_ar.dim[0].ubound; atmp.1.dtype = 265; atmp.1.data = 0B; atmp.1.offset = 0; g (&atmp.1); g() is called with a unallocated temporary variable, so your allocation in g() works. The next several lines copy atmp into loc_ar. D.1398 = NON_LVALUE_EXPR <D.1392>; { integer(kind=8) S.2; S.2 = 0; while (1) { if (atmp.1.dim[0].ubound - atmp.1.dim[0].lbound < S.2) goto L.2; (*D.1390)[(S.2 + D.1398) + D.1391] = (*(integer(kind=4)[0:] * restrict) atmp.1.data)[S.2]; S.2 = S.2 + 1; } L.2:; } Here, the temporary variable is destroyed. { void * D.1397; D.1397 = (void *) atmp.1.data; if (D.1397 != 0B) { __builtin_free (D.1397); } } } } I need to go read the Fortran standard to determine the semantics of a function returning an allocatable entity. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42112