Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28809 Discussions

Pointer initialized to NULL() in routine declaration, does that imply SAVE?

Scott_Boyce
New Contributor I
611 Views

Given a pointer variable within a routine declaration that is initialized to null, does that imply the save attribute?

 

For example, the following implies SAVE in the variable mxval and nullifies tmp.

Since I don't nullify tmp at the exit of the subroutine, does it retain its pointer association despite the null() on line 4:

subroutine set_pnt(pnt, val)
  integer, pointer:: pnt
  integer:: val
  integer, pointer:: tmp => null()
  integer:: mxval = 99
  !
  allocate(tmp, source=val)
  pnt => tmp
  !
end subroutine

To me it would seem better to have the first line in the subroutine say

nullify(tmp)

to remove the implied SAVE.

 

Or is SAVE only implied for non-pointer variables?

 

Also, is tmp is auto-disassociated when the subroutine exits or is that behavior not guaranteed?

0 Kudos
1 Reply
Steve_Lionel
Honored Contributor III
593 Views

There is no restriction on an initialized variable being pointer, it is SAVEd (unless in a COMMON block).  On reentry to the routine, it will have whatever association it had on the last exit. Since your first action is to allocate tmp, I don't see a point in initializing it.  Otherwise, I would do away with the initialization and add the explicit nullify.

If tmp did not have the SAVE attribute, then on exit its association status becomes undefined (it is not nullified.) It would also be undefined on entry.

Reply