(j3.2006) deallocating pointer function results
Robert Corbett
robert.corbett
Tue Nov 3 05:34:14 EST 2015
The following message is based on a thread on comp.lang.fortran.
All references are with respect to Fortran 2008.
Consider the following program:
!-----------------------------------------------------------------
program main
deallocate( new_int() ) ! (1)
call my_deallocate( new_int() ) ! (2)
call hmm_deallocate( new_int() ) ! (3)
contains
function new_int()
integer, pointer :: new_int
allocate(new_int)
new_int = 3
end function
subroutine my_deallocate(p)
integer, pointer :: p
deallocate( p )
end subroutine
subroutine hmm_deallocate(p)
integer, pointer :: p, q
q => p
deallocate( q )
end subroutine
end
!-----------------------------------------------------------------
The statement marked (1) is clearly not permitted, because it
violates syntax rule R632.
The statement marked (2) leads to prohibited behavior.
Subclause 5.3.10, paragraph 5, states
If no INTENT attribute is specified for a dummy argument,
its use is subject to the limitations of its effective
argument (12.5.2).
Because a function reference is not allowed as an
/allocate-object/ in a DEALLOCATE statement, a dummy argument
corresponding to an actual argument that is a function reference
is not allowed as an /allocate-object/ in a DEALLOCATE statement.
(I doubt that any existing implementation catches this error.)
I think that the behavior that results from the statement
marked (3) is intended not to be permitted. I base my
conclusion on a somewhat strained reading of Subclause 12.5.2.13,
paragraph 1, which states
While an entity is associated with a dummy argument,
the following restrictions hold.
(1) Action that affects the allocation status of
the entity or a subobject thereof shall be
taken through the dummy argument.
One issue concerns the meaning of the phrase "the allocation
status". I am assuming that it means "the allocation status or
pointer association status". In all other cases where both the
allocation status and the pointer allocation status are meant,
they are both mentioned. A reason to believe that both are
intended here is that the text in the Fortran 90 standard
(Subclause 12.5.2.9 of the Fortran 90 standard) from which the
quoted text was derived was described in a way that included
both cases.
The dummy argument is not used in the DEALLOCATE statement; a
local pointer variable is used in its place. I assume that the
deallocation is therefore not done "through" the dummy argument.
An argument can be made against my assumption based on
Subclause 5.3.10, paragraph 2. It states
The INTENT(IN) attribute for a pointer dummy argument
specifies that during the invocation and execution of
the procedure its association shall not be changed
except that it may become undefined if the target is
deallocated other than through the pointer (16.5.2.5).
If the "allocation status" of the pointer may be changed only
through the dummy argument, then the last part of that clause
is unnecessary.
I would like to know if anyone disagrees with my conclusions.
Robert Corbett
More information about the J3
mailing list