I have a code similar to this:
module mod
implicit none
real, pointer :: p1=>null(), p2=>null()
end module mod
program target_lifetime
use mod
implicit none
real, target :: t(2)
p1 => t(1)
p2 => t(2)
nullify( p1, p2 )
end program target_lifetime
When I compile this code with
gfortran -Wall target_lifetime.f90
I obtain
target_lifetime.f90:14:4:
14 | p1 => t(1)
| 1
Warning: Pointer at (1) in pointer assignment might outlive the pointer target [-Wtarget-lifetime]
target_lifetime.f90:15:4:
15 | p2 => t(2)
| 1
Warning: Pointer at (1) in pointer assignment might outlive the pointer target [-Wtarget-lifetime]
What is the correct way to code this in order to get rid of the warning?
The warning here is clearly bogus because of the lifetime of the main program variable is until the end of the program and the t
array is implicitly save
.
There is not much you can do. You can open a bug with GCC and ask whether it is possible to change the behaviour here.
I can see that it could be triggered by internally implementing the __MAIN
(or some other name) procedure as a subroutine (actually a funciton in C or GIMPLE) and then treating the variables as local variables in some checks. But then the standard still says that the variables in the main program are save
implicitly.