flang icon indicating copy to clipboard operation
flang copied to clipboard

Explicitly initialized pointers should acquire the SAVE attribute implicitly

Open bryanpkc opened this issue 4 years ago • 0 comments

Section 8.4 ("Initialization") of the Fortran 2018 standard states:

If null-init appears, the initial association status of the object is disassociated. If initial-data-target appears, the object is initially associated with the target.

Explicit initialization of a variable that is not in a common block implies the SAVE attribute, which may be confirmed by explicit specification.

Flang supports pointer initialization, but does not give such pointers the SAVE attribute, as shown by the example below:

program test_pointer_init
  call test_sub() ! expected "F", got "F"
  call test_sub() ! expected "T", got "F"
contains
  subroutine test_sub()
    implicit none
    integer, pointer :: ptr => NULL()
    logical :: initialized
    initialized = associated(ptr)
    if (.not. initialized) allocate(ptr)
    print *, initialized
  end subroutine
end program

The programmer can work around this by explicitly adding the SAVE attribute to the declaration of ptr.

bryanpkc avatar Feb 06 '21 21:02 bryanpkc