# Avoid temporary arrays when using derived types

**URL:** <https://fortran-lang.discourse.group/t/avoid-temporary-arrays-when-using-derived-types/2226>\
**Category:** Help\
**Created:** [November 3, 2021, 2:32pm UTC](https://fortran-lang.discourse.group/t/avoid-temporary-arrays-when-using-derived-types/2226 "2021-11-03T14:32:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [November 3, 2021, 2:32pm UTC](https://fortran-lang.discourse.group/t/avoid-temporary-arrays-when-using-derived-types/2226/1 "2021-11-03T14:32:19Z")

</div>

Suppose that I have a subroutine that revises arrays `A` and `B` by some intensive computation:

```auto
subroutine foo(A, B)
real, intent(inout) :: A(:, :), B(:, :)
! Do something intensive to revise A, B
end subroutine foo

```

Now, I would like to define a derived type `DT` consisting of `A` and `B` and rewrite `foo` into `bar` as follows.

```auto
subroutine bar(D)
type(DT), intent(inout) :: D
! Do something intensive to revise D%A, D%B
end subroutine bar

```

Here comes a problem. The calculation that revises `A` and `B` is truly intensive. So it would be cumbersome to write always `D%A` and `D%B`. To solve this problem, we may define temporary arrays `A=D%A` and `B=D%B`, work on `A` and `B`, and then set `D%A=A`, `D%B=B` before the end of the subroutine. However, if `A` and `B` are huge, this would not be ideal. So, what is the best practice in this situation?

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [November 3, 2021, 2:38pm UTC](https://fortran-lang.discourse.group/t/avoid-temporary-arrays-when-using-derived-types/2226/2 "2021-11-03T14:38:28Z")

</div>

> [@zaikunzhang](#):
>
> … what is the best practice in this sutuation?

@zaikunzhang ,

Look into `ASSOCIATE` construct:

```Fortran
subroutine bar(D)
   type(DT), intent(inout) :: D
   associate ( A => D%A, B => D%B )
      ! Do something intensive to revise D%A, D%B via associate-names of A and B!
   end associate
end subroutine bar

```

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [November 3, 2021, 3:11pm UTC](https://fortran-lang.discourse.group/t/avoid-temporary-arrays-when-using-derived-types/2226/3 "2021-11-03T15:11:15Z")

</div>

How does one decide between the solution above and an internal subroutine, as illustrated below? I assume compilers would not make copies of the  
`intent(in out)` arguments.

```auto
subroutine bar(D)
   type(DT), intent(inout) :: D
   call foo(D%A,D%B)
   contains
   subroutine foo(A,B)
   real, intent(in out) :: A(:,:), B(:,:)
   ! modify A and B
   end suboutine calc
end subroutine bar

```

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [November 3, 2021, 3:52pm UTC](https://fortran-lang.discourse.group/t/avoid-temporary-arrays-when-using-derived-types/2226/4 "2021-11-03T15:52:19Z")

</div>

I would not even make the subroutine `foo` internal. I like to create such standalone procedures as much as possible that operate on arrays, no derived types. This makes them very reusable in all kinds of settings.

Then in the upper calls if it makes sense to use a derived type, then the member arrays can be passed as arguments as shown above.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [November 4, 2021, 1:00am UTC](https://fortran-lang.discourse.group/t/avoid-temporary-arrays-when-using-derived-types/2226/5 "2021-11-04T01:00:19Z")

</div>

Thank @FortranFan, @Beliavsky, and @certik all of you for the illuminating suggestions. I believe that each of them is quite useful under certain situations. I understand this problem better now. Considering my code, I guess I would more or less follow what @certik suggests, namely to implement the true calculations on arrays, and then use derived types only as “interfaces” between different components of the program.

Many thanks again!
