2 ! { dg-options "-fdump-tree-original" }
6 ! The 'to' components of 'mytemp' would remain allocated after the call to
7 ! MOVE_ALLOC, resulting in memory leaks.
9 ! Contributed by Alberto Luaces.
11 ! See https://groups.google.com/forum/#!topic/comp.lang.fortran/k3bkKUbOpFU
15 integer, allocatable
:: i(:)
16 end type myallocatable
19 subroutine f(num
, array
)
21 integer, intent(in
) :: num
23 type(myallocatable
):: array(:)
26 allocate(array(i
)%i(5), source
= [1,2,3,4,5])
35 type(myallocatable
), allocatable
:: myarray(:), mytemp(:)
36 integer, parameter:: OLDSIZE
= 7, NEWSIZE
= 20
39 allocate(myarray(OLDSIZE
))
40 call f(size(myarray
), myarray
)
42 allocate(mytemp(NEWSIZE
))
43 mytemp(1:OLDSIZE
) = myarray
50 if (allocated (mytemp
)) deallocate (mytemp
)
52 allocate(myarray(OLDSIZE
))
53 call f(size(myarray
), myarray
)
55 allocate(mytemp(NEWSIZE
))
56 mytemp(1:OLDSIZE
) = myarray
58 ! Verfify that there is no segfault if the allocatable components
59 ! are deallocated before the call to move_alloc
70 deallocate (mytemp(i
)%i
)
73 call move_alloc(mytemp
, myarray
)
79 if (.not
.flag
.and
. allocated (myarray(i
)%i
)) then
80 if (any (myarray(i
)%i
.ne
. [1,2,3,4,5])) call abort
82 if (.not
.flag
) call abort
87 ! { dg-final { scan-tree-dump-times "__builtin_malloc" 11 "original" } }
88 ! { dg-final { scan-tree-dump-times "__builtin_free" 11 "original" } }