(j3.2006) deallocating pointer function results

Robert Corbett robert.corbett
Tue Nov 3 18:08:07 EST 2015


On 11/03/15 06:17, Bill Long wrote:
> On Nov 3, 2015, at 4:34 AM, Robert Corbett<robert.corbett at oracle.com>  wrote:
>
>> 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.
> Agreed.  I had to comment out this line to get the program to compile and run.
>
>> 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 don?t agree with this.  The actual argument is a function reference that returns a data pointer, which by our definitions, is a ?variable? (R602).   The actual argument looks like any other associated data pointer for the purposes of argument association.

The actual argument in this example must be a pointer.
Subclause 12.5.2.7, paragraph 2, states

       If the dummy argument does not have the INTENT(IN), the
       actual argument shall be a pointer.

Subclause 6.2, paragraph 1, states

       A variable is either the data object denoted by
       /designator/ or the target of /expr/.

The target of the data pointer returned by new_int() is the
anonymous integer variable allocated by new_int().  It is not
a pointer, and so it cannot be the actual argument.  The
data pointer returned by new_int() is a pointer and so it can
be the actual argument, but it is not a variable.

Robert Corbett





More information about the J3 mailing list