Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28445 Discussions

Assign allocated array to other allocatable array

Paweł_J_
Beginner
360 Views

Hi,

I've got two codes, which I want to combine. In one code I've got type:

  TYPE, PUBLIC ::m_type
    INTEGER :: m,
    REAL ( KIND( 1.0D+0 ) ), ALLOCATABLE, DIMENSION(:) :: val
  END TYPE

and subroutine which uses this type as an input:

   subroutine sub(m)
      type(m_type), intent(in) :: matrix
      ....
   end subroutine

In the second code, which calls the subroutine sub(), I have already allocated array, let say x(:), and I don't want to allocate once more memory for m%val, just for copying data, because size of x(:) is huge. Is there any way to assign m%val with values of x(:) without allocating additional memory? In C pointer will do the trick, but I don't know how I could use it here. I dont want to change subroutine sub(), because it will cause changes not only in my part of the code.

Any ideas?

0 Kudos
2 Replies
mecej4
Honored Contributor III
360 Views

See if MOVE_ALLOC can be made to do the work for you.

0 Kudos
jimdempseyatthecove
Honored Contributor III
360 Views

Fortran has pointers

module m_mod
TYPE, PUBLIC ::m_type
  INTEGER :: m,
  REAL ( KIND( 1.0D+0 ) ), ALLOCATABLE, POINTER, DIMENSION(:) :: val
END TYPE
end module m_mod

...
real, kind(1.0D+0), allocatable, target :: x(:)
...
Allocate(x(isHuge))
...
call sub(m)
...
subroutine sub(m)
use m_mod
type(m_type) :: m
...
m%val => x ! or x(from:to)

Your post was not clear as to if sub were initializing m%val or using. The above shows part of initializing.

*** Just as in C/C++, it is your responsibility to assure that the underlying x is not deallocated (or reallocated) during the lifetime of pointers pointing to (portions of) it.

Jim Dempsey

0 Kudos
Reply