Using move_alloc in a derived type

528 views Asked by At

I have written a derived type that stores a multidimensional array. The array will be read from a file to an array b. I then want to transfer b to the derived type Space using member a. The advantage for mvalloc is not having to allocate two large arrays which might create memory problems.

When I compile the code I am getting the error

Call Move_Alloc (b, t% a)
               1
Error: 'from' argument of 'move_alloc' intrinsic 
at (1) must be ALLOCATABLE

The type definition and relevant subroutine follows

Type, Public :: Space

  Character (Len=65) :: nm
  Real, Allocatable :: a(:,:,:) 

  Contains
    Procedure :: mvalloc => space_mvalloc

End Type Space


Subroutine space_mvalloc (t, b)

  Class (Space), Intent (InOut) :: t
  Real, Intent (InOut) :: b(:,:,:)

  Call Move_Alloc (b, t% a)

End Subroutine space_mvalloc
1

There are 1 answers

4
francescalus On BEST ANSWER

As the error message says, the arguments to move_alloc must be allocatable. That's both of them. from is the first, and this must be allocatable for the allocation to move from it.

You have

Subroutine space_mvalloc (t, b)

  Class (SField), Intent (InOut) :: t
  Real, Intent (InOut) :: b(:,:,:)

  Call Move_Alloc (b, t% a)
End Subroutine space_mvalloc

b here is not allocatable. You should add that attribute, and ensure that the actual argument passed in is allocatable.

After the call to move_alloc b, and the corresponding actual argument, will be unallocated.