Hello everyone ![]()
so, it has been said few times, here and on other platforms as well, that if one could avoid the use of pointers in Fortran, then maybe it should, and live a good and safe life with allocatables.
Indeed, pointers have their advantages, provided the fact that the user knows exactly in which case he/she is using them, and how. Otherwise, I see no reason for them to be part of the language features.
Now, consider this:
program main
implicit none
integer, target, allocatable :: ivar(:)
call sub(null())
call sub(ivar)
ivar = [1, 2, 3, 4, 5]
call sub(ivar)
contains
subroutine sub(ivar)
integer, intent(in), pointer, contiguous, allocatable :: ivar(:)
if (associated(ivar)) then
if (allocated(ivar)) then
print '(*(i0, " "))', ivar
else
print *, ' Target not allocated !'
endif
else
print *, ' Pointer not associated !'
endif
end subroutine
end program
GFortran does not compile at all, saying that pointer attribute is not compatible with allocatable.
While both ifort and ifx do compile, but the treat the first call sub(ivar) as call sub(null()).
Basically, you cannot have a pointer to an allocatable variable, keeping the two āconceptsā distinct.
For GFortran, I think because you could allocate a pointer, i.e. allocate(ivar) in the subroutine, which makes ivar associated with an anonymous variable.
ifort and ifx they implement it as ivar being not associated (in the subroutine). (NOTE: in such case, one might argue that it does not change the expected behavior, finally. Though itās not exactly āmyā expected one, still GFortran does not compile it. So, yes, it still remain a concern.)
Here it sound back the voice saying āif you could avoid usign pointers, do it, and use allocatables insteadā.
Well, of course one could. But, assuming that sub() is a procedure exposed in an API, the use of allocatables would force the user to anyway declare that allocatable actual argument, which might or might not be allocated, to be able to make the procedure call. This cancels out the possibility of avoiding declaring it and passing null() in those cases when it knows for sure it will not need that entry in the procedure call.
Another important thing that one might argue on, is the line if (allocated(ivar)) in sub().
Here, it would makes NO sense to inquire for allocation status of a pointer dummy argument. Because pointers can only be associated, right? But, here is where it would kick in the implicit pointer dereferentiation that exist in Fortran. So that it would implicitly mean if (allocated(ivar->target)). At this point, if the actual argument is not actually an allocatable, then thow a (compile-time?) error. Otherwise, inquiry for its allocation status.
(NOTE: well, this is what actually happens with ifort and ifx, but only when the actual argument is allocated. Otherwise, they threat the dummy pointer as not being associated)
At this point, I donāt have in mind any workaround for obtaining the same desired (cross-compiler compliant) outcome.
If youād have any suggestion, please kindly let me know ![]()