move_alloc for types holding allocatable members

Anton Shterenlikht mexas@bris.ac.uk
Thu Oct 8 19:35:00 GMT 2015


>program name
>  use alloctest
>  implicit none
>  type(myallocatable), allocatable:: myarray(:), mytemp(:)
>
>  allocate(myarray(7))
>  call f(size(myarray), myarray)
>
>  myarray(3)%i = 6
>  print *, myarray(3)%i
>
>  allocate(mytemp(20))
>  call move_alloc(mytemp, myarray)
>
>  print *, myarray(3)%i
>
>  deallocate(myarray)
>end program name

Perhaps like this:

rat> cat z.f90
program name
  use alloctest
  implicit none
integer :: ind
  type(myallocatable), allocatable:: myarray(:), mytemp(:)
  allocate( myarray(7) )
  call f( size( myarray ), myarray )
  myarray(3)%i = 6
  print *, myarray(3)%i
call move_alloc( myarray, mytemp )
  print *, mytemp(3)%i
deallocate( mytemp )
end program name

rat> ./a.out
           6           6           6           6           6
           6           6           6           6           6


Anton



More information about the Fortran mailing list