# Pointer vs Allocatable With Arjen

**URL:** <https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325>\
**Category:** Tutorials\
**Created:** [October 2, 2020, 1:51pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325 "2020-10-02T13:51:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [October 2, 2020, 1:51pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/1 "2020-10-02T13:51:30Z")

</div>

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

[![](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/0a6c143adf8546779b3576ad2dc7a1f210e51bba.jpeg "Fortran Pointer Vs Allocatable With Arjen Markus") ](https://www.youtube.com/watch?v=hPBGpyX--W8)

---

<div class="post-metadata">

**Author:** ![milancurcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/milancurcic/32/2_2.png) [@milancurcic](https://fortran-lang.discourse.group/u/milancurcic)\
**Post date:** [October 2, 2020, 7:54pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/2 "2020-10-02T19:54:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [October 2, 2020, 8:04pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/3 "2020-10-02T20:04:05Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 2, 2020, 8:25pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/4 "2020-10-02T20:25:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [October 2, 2020, 8:37pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/5 "2020-10-02T20:37:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 2, 2020, 9:36pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/6 "2020-10-02T21:36:59Z")

</div>

I’ll be happy to. 🙂

---

<div class="post-metadata">

**Author:** ![lkedward](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lkedward/32/72_2.png) [@lkedward](https://fortran-lang.discourse.group/u/lkedward)\
**Post date:** [October 3, 2020, 5:21pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/7 "2020-10-03T17:21:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jeremie.vandenplas](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jeremie.vandenplas](https://fortran-lang.discourse.group/u/jeremie.vandenplas)\
**Post date:** [October 3, 2020, 6:14pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/8 "2020-10-03T18:14:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nshaffer](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nshaffer/32/161_2.png) [@nshaffer](https://fortran-lang.discourse.group/u/nshaffer)\
**Post date:** [October 5, 2020, 2:14pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/9 "2020-10-05T14:14:13Z")

</div>

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.,

```auto
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`

```auto
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.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [October 5, 2020, 5:11pm UTC](https://fortran-lang.discourse.group/t/pointer-vs-allocatable-with-arjen/325/10 "2020-10-05T17:11:02Z")

</div>

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](https://www.computer.org/csdl/magazine/cs/2012/02/mcs2012020046/13rRUxCitDc)
