codee-com / codee-com/open-catalog

[Fortran] replace pointer with allocatable

Open
#34 1 comment 2 reactions 0 assignees View on GitHub
Fortran good first issue new check
Dominant language
Fortran
Stars
106
Forks
12
PR merge metrics
No merged PRs in 30d

Description

In Fortran, it is possible to create an array with
```fortran
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
```fortran
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
```fortran
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
```fortran
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
```fortran
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.