[J3] Question about the SAVE attribute

Daniel Chen cdchen at ca.ibm.com
Thu May 29 15:09:40 UTC 2025


Hello,

Consider the following code:

```
module m
    contains
        subroutine test(arg)
            real, save, pointer :: ptr
            real, optional, target :: arg           !! What if I added VALUE attribute here?

            if ( present(arg) ) then
                ptr => arg
            else
                print*, ptr
            endif

        end

end module

program main

    use m

    call test(4.0 + 5.0)
    call test()

End program```

The local pointer `ptr` has the SAVE attribute. It is associated with the target dummy argument `arg` when subroutine `test` is called the first time.

Should it preserve the association status at the 2nd call to `test`?

It seems the compilers I have access to indeed saved the pointer association status and prints `9.0`. But after I also added the VALUE attribute to `arg`, some compiler still saves the status but some don’t.

What is the standard conforming behavior here?

Thanks,
Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20250529/cb26e66e/attachment.htm>


More information about the J3 mailing list