Pointer vs Allocatable With Arjen

Arjen and I had a discussion about pointer and allocatable variables. You can watch our talk on YouTube.

9 Likes

I enjoyed this conversational format very much, do more of that.

I’m glad you liked it. I’ve already got an idea for a follow up, so I’ll keep going so long as I can keep getting volunteers (hint hint to anybody interested in contributing).

1 Like

I like this. The other thing you can do is interviews with members of the community and have a channel at youtube (or elsewhere). I think this can greatly help getting to know each other better.

I like the idea. Maybe if you get the J3 Chair position you might volunteer for the first one? :wink:

1 Like

I’ll be happy to. :slight_smile:

1 Like

Interesting and insightful conversation! Thanks for sharing.

Regarding the deallocation of pointers in finalisers, the problem raised in your conversation is solved by reference counting in defined assignment; this allows you to return such objects from functions without having the pointer deallocated since the assignment necessarily occurs before the function result finaliser is called.

I like this format and the conversation. Thank you for sharing it.

Nice! I came into it expecting something with more structure rather than a softly guided conversation, but once I adjusted that expectation, I quite liked it. Thanks Arjen and Brad!

In the code that I’ve read, the most common use of pointers has been to give a name to an array section to make it convenient to assign to or use in expressions, e.g.,

implicit none
real, target :: r(3, 5)
real, pointer :: x(:), y(:), z(:)
integer :: i
x => r(1,:)
y => r(2,:)
z => r(3,:)
x = [(1.0*i, i=1,size(x))]
y = [(10.0*i, i=1,size(y))]
z = [(100.0*i, i=1,size(z))]
print *, r ! 1.0 10.0 100.0 2.0 20.0 200.0 ...
nullify (x, y, z)
end

But I suspect that kind of usage happens when one doesn’t know about associate

implicit none
real :: r(3, 5)
integer :: i
associate (x => r(1,:), y => r(2,:), z => r(3,:))
  x = [(1.0*i, i=1,size(x))]
  y = [(10.0*i, i=1,size(y))]
  z = [(100.0*i, i=1,size(z))]
end associate
print *, r ! 1.0 10.0 100.0 2.0 20.0 200.0 ...
end

Of course, the pointer version lets x, y, and z cross procedure boundaries, but the chat did a nice job of highlighting that this can introduce subtle bugs, especially if the target array is an allocatable.

1 Like

I’m glad you liked it. I’m actually planning to record a discussion with Damian Rouson about this exact approach, as he recently sent me a paper he wrote a while back doing just that.

This Isn’t Your Parents’ Fortran: Managing C++ Objects with Modern Fortran

3 Likes

The background is Xuanwu Lake, Nanjing, China. Also that mountain (purple mountain) I see every day through my window. It is cool to see the same view when doing Fortran through Arjen’s book and now by listening to this conversation. :smiley: :smiley: :smiley: