I'm trying to learn Fortran2018 using gfortran.
When playing around with pointers I noticed that there doesn't seem to be a facility to test for nullpointers. So I have two questions:
- Is there really no (straightforward) way to test if a pointer is a nullpointer or "ready to use" in Fortran?
- If there isn't, why wasn't it considered necessary in Fortran so far?
More practically speaking, in the code snippet below: How could we find out at any point during execution if assigning to p was "safe" or not? (Possibly imagining a more complex sequence of allocate, nullify, and deallocate statements.)
program test
      implicit none
      real, pointer :: p
      ! p = 333.333 ! Segfault, p is neither defined, nor allocated
      ! Since p is not defined, checking whether it's 
      ! associated to a target gives arbitrary results:
      print *, "Start: Pointer p associated? ", associated(p) ! Result: True or False
      nullify(p) ! Now p is defined, but `null`
      print *, "Nullyfied: Pointer p associated? ", associated(p) ! False
      ! p = 123.456 ! Still a segfault
      allocate(p) ! Now p can be accessed
      print *, "Allocated: Pointer p associated? ", associated(p) ! True
      p = 987.654 ! Now assignment is possible
      
      allocate(p) ! Loses the value pointed to by p.
      print *, p ! Result: 0.00000000
      deallocate(p) ! Now accessing p generates a segfault again.
      print *, "Deallocated: Pointer p associated? ", associated(p) ! False
      ! Never allowed:
      ! allocated(p)
      ! p == null()
      ! p .eqv. null()
      end program test
 
     
    