Among several things, pointers can be used to represent an array from a specific shape into another one. It can be useful for certain situations. However, the use of pointers is generally not recommended. This is due to potential memory management risks.

For instance, It is important to keep the same size when remapping arrays using pointers. Overall, the memory must be consistent.

Let’s say, it is required to represent a 2D array into a 3D:

  • 2D array: shape of (2,12) has (2*12 =) 24 positions
  • 3D array: shape (2,3,4) has (2*3*4 =) 24 positions

As expected, both arrays have the same number of positions. Nonetheless, those have a different memory layout.

In order to remap an array in Fortran, you must following the syntax shown below:

real, pointer, contiguous  :: my_data_pointer(:,:,:)
real :: my_data(Mdim1, Mdim2)
...
my_data_pointer(1:Ndim1, 1:Ndim2, 1:Ndim3) => my_data(Mdim1,Mdim2)

Full example:

program main

implicit none

integer :: expected(2,3,4) !< to compare with
integer, target :: data(2,3*4) !< store data
integer, contiguous, pointer :: d(:,:,:) !< 2,3,4 !< use contiguous for efficiency 
integer :: counter
integer :: i,j,k

!< define pointer remapping
d(1:2,1:3,1:4) => data(:,:)
counter = 0

!< init values from 0 to N
do i=1, 4
  do j=1,3
    do k=1, 2
      d(k,j,i) = counter
      expected(k,j,i) = counter
      write(*,*) "main:: i,j,k, value=", i,j,k,counter
      counter = counter + 1
    enddo !< k=2
  enddo !< j=3
enddo !< i=4

write(*,*) "data=", d
write(*,*) "expected == pointer ?", all(expected == d)


end program main

Output:

gfortran main.f90 && ./a.out
 main:: i,j,k, value=           1           1           1           0
 main:: i,j,k, value=           1           1           2           1
 main:: i,j,k, value=           1           2           1           2
 main:: i,j,k, value=           1           2           2           3
 main:: i,j,k, value=           1           3           1           4
 main:: i,j,k, value=           1           3           2           5
 main:: i,j,k, value=           2           1           1           6
 main:: i,j,k, value=           2           1           2           7
 main:: i,j,k, value=           2           2           1           8
 main:: i,j,k, value=           2           2           2           9
 main:: i,j,k, value=           2           3           1          10
 main:: i,j,k, value=           2           3           2          11
 main:: i,j,k, value=           3           1           1          12
 main:: i,j,k, value=           3           1           2          13
 main:: i,j,k, value=           3           2           1          14
 main:: i,j,k, value=           3           2           2          15
 main:: i,j,k, value=           3           3           1          16
 main:: i,j,k, value=           3           3           2          17
 main:: i,j,k, value=           4           1           1          18
 main:: i,j,k, value=           4           1           2          19
 main:: i,j,k, value=           4           2           1          20
 main:: i,j,k, value=           4           2           2          21
 main:: i,j,k, value=           4           3           1          22
 main:: i,j,k, value=           4           3           2          23
 data=           0           1           2           3           4           5           6           7           8           9          10          11          12          13          14          15          16          17          18          19          20          21          22          23
 expected == pointer ? T

Finally, the comparison between the expected array versus the pointer prove to have the same values.

Leave a Reply

Your email address will not be published. Required fields are marked *