2

Is it valid to do something like:

real(kind=rk), allocatable, target :: arr(:,:)
real(kind=rk), pointer :: ptr(:,:)

allocate(arr(10, 10))
ptr => arr(5:7, 5:7)

arr = 0
ptr(-1, 4) = 1

e.g. create pointer to array subsection and then access indices that are outside the subsection, but exist in the original array?

2 Answers2

1

An array with the pointer attribute is still an array in its own right, with its own bounds. It is not valid to attempt to access an array element outside these bounds.

In the case of a pointer, such access may "work" - the program still owns the memory. This is not valid, though.

francescalus
  • 30,576
  • 16
  • 61
  • 96
0

Since Fortran 2003 it is possible to use bounds-spec in pointer assignment:

real(kind=rk), allocatable, target :: arr(:,:)
real(kind=rk), pointer :: ptr(:,:)

allocate(arr(10, 10))
ptr(-1:,-1:) => arr(3:8, 3:8) 

arr = 0
ptr(-1, 4) = 1
  • This is true, but not the intention of the OP. Your code is actually different then what the OP intended. – kvantour Apr 20 '18 at 09:37