open-catalog icon indicating copy to clipboard operation
open-catalog copied to clipboard

[Fortran] replace pointer with allocatable

Open RRiva opened this issue 1 year ago • 1 comments

In Fortran, it is possible to create an array with

real*8, dimension(:), pointer :: vec
allocate(vec(100))

the effect of this code is to allocate an array, and associate the pointer vec to it. Unfortunately, since it is a pointer, it will not be automatically deallocated when it goes out of scope, and therefore could cause a memory leak. A safer way of allocating an array is

real*8, dimension(:), allocatable :: vec2
allocate(vec2(100))

Unfortunately, it is not just a matter of replacing pointer with allocatable, since there are also other steps.

First of all, we must distinguish, which arrays can become allocatable from the ones that must remain pointers. Consider the following example

real*8, dimension(:), pointer :: vec
real*8, dimension(:), pointer :: p_vec
allocate(vec(100))
p_vec => vec(20:50)

In this case, only vec should become an allocatable.

Since it is good practice to nullify pointers (otherwise their status is undefined), there might be a bunch of vec=>null() and nullify(vec), which must be deleted.

Next, the code might contain several if (associated(vec)), which must be replaced with if (allocated(vec)).

The last thing to address is that when a pointer is associated to an allocatable, that allocatable needs the target attribute. For example

real*8, dimension(:), allocatable, target :: vec2
real*8, dimension(:), pointer :: p_vec
allocate(vec2(100))
p_vec => vec2(20:50)

A slightly more complex case is

type t_special
    real*8 :: a
    real*8, dimension(:), allocatable :: b
end t_special

type(t_special), dimension(:), allocatable, target :: c
type(t_special), dimension(:), pointer :: p_c

allocate(c(15))
p_c => c

Note that in this case the target attribute is applied to c, and not to the fields of the derived type.

RRiva avatar Sep 19 '24 07:09 RRiva

Hi @RRiva,

Thank you once again for your contribution to improving the Open Catalog, and for providing such detailed explanations!

Following our conversation from #36, we fully agree on the many advantages of using Fortran's allocatable. It's a key part of the modernization steps that the Fortran community highlighted while creating the Top 10 Recommendations for Fortran Modernization initiative.

If you'd like, feel free to submit a PR proposing an entry in the Open Catalog for a new check on using allocatable. You can find some tips to get started in the conversation on #36. Of course, if you’d prefer not to, that’s totally fine! We’ll prioritize this check and keep the issue open until it’s been added :)

Thank you again for your contributions to improving this project!

alvrogd avatar Sep 20 '24 11:09 alvrogd