Reputation: 4771
In Fortran one can allocate memory to a pointer, or one cannot:
real(kind=jp), target :: bt(100,100)
real(kind=jp), pointer :: pt(:,:)
But then you can allocate memory to the pointer pt
:
allocate(pt(100,100))
My question is: what are the pros and cons? For all I can see, allocating memory to the pointer defeats the purpose of a pointer and uses up more memory. Granted, my knowledge of pointers is limited so if some could explain to me what going on here, I would grateful.
I'm using a model with mixed FORTRAN 77 and Fortran 90 code, plus, I'm compiling the code using Intel compilers.
Upvotes: 2
Views: 3724
Reputation: 32396
As Vladimir F said in a comment to another answer, there may well be some confusion as to what the example code is doing. Although not a wide answer all about pointers (which is already partly covered), I'll comment briefly.
real(kind=jp), target :: bt(100,100)
real(kind=jp), pointer :: pt(:,:)
The pointer pt
is not at this point pointing to anything: it has undefined association status. That you have the two objects declared together, one with the target
attribute and one with the pointer
attribute, doesn't signify something.
To associate pt
with the target bt
, pointer assignment is required:
pt => bt
In this way, one can go ahead and do things to bt
through the pointer pt
without any extra memory allocation (there will be some overhead associated with the pointer variable, but let's ignore that).
Yes, one also could do pointer allocation as in the question
allocate(pt(100,100))
but this newly minted lump of memory has nothing to do with bt
.
As Alexander Vogt said in modern times there are reduced reasons to want to do that sort of thing (using allocatable arrays instead is a good thing to consider).
In summary, for "what are the pros and cons?": pointer assignment and allocation do totally different things. Choose whichever is appropriate.
Upvotes: 6
Reputation: 18118
Allocatable pointers as you use them were widely in use before dummy arguments could be allocatable
(Fortran 2003). This way you could pass an array and allocate it in another routine, which is immensely helpful for input routines where you don't know the input size before-hand.
Allocated pointers behave very similarly to the usual counterparts, but are not automatically deallocated. Some say that using pointers this way is detrimental on performance, but I have never experienced this myself (probably compiler specific). One issue with pointers, though, is possible aliasing when used as dummy arguments.
Nowadays, I would not use pointers when I could use an allocatable
array instead. This has many benefits, key among them is the automatic deallocation and better readability of the code. (If you like, you can also use automatic allocation of the LHS, but I usually turn that off).
For OOP pointers are essential and I use them a lot for e.g. linked lists, trees, etc. If you have nested derived types, it is also quite elegant to use pointers to reduce de-referencing.
Upvotes: 4