Running the testcase using valgrind --leak-check=full --track-origins=yes:
==28585== Conditional jump or move depends on uninitialised value(s)
==28585== at 0x400E19: MAIN__ (pdt_48.f03:48)
==28585== by 0x400EDB: main (pdt_48.f03:34)
==28585== Uninitialised value was created by a heap allocation
==28585== at 0x4841984: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28585== by 0x400975: __pdt_m_MOD_add (pdt_48.f03:30)
==28585== by 0x400D84: MAIN__ (pdt_48.f03:44)
==28585== by 0x400EDB: main (pdt_48.f03:34)
The cause was a partial initialization of a vector used in a subsequent
addition. Initialize the remaining elements of the first vector by zero.
gcc/testsuite/ChangeLog:
* gfortran.dg/pdt_48.f03:
integer :: c
c=max(a,b)
end function diy_max
-
+
function add(a,b) result(c)
type(vec(k=*)), intent(in) :: a,b
type(vec(k=max(a%k,b%k))) :: c ! Fails
!type(vec(k=diy_max(a%k,b%k))) :: c ! Worked with diy_max
!type(vec(k=a%k+b%k)) :: c ! Worked with +
-
+
c%foo(1:a%k)=a%foo
+ c%foo(a%k+1:) = 0
c%foo(1:b%k)=c%foo(1:b%k)+b%foo
- if (c%k /= 5) stop 1
+ if (c%k /= 5) stop 1
end function add
end module pdt_m