(j3.2006) a question on c_f_pointer call

Bill Long longb
Wed Aug 25 15:18:40 EDT 2010



Jim Xia wrote:
> 
> We're receiving a customer question on pointer's deallocation status for 
> FPTR from intrinsic c_f_pointer().  Basically the question is whether 
> user should expect successful deallocation of FPTR from call 
> c_f_pointer.  This looks pretty much processor dependent since I can't 
> find clear wording in the standard specifying the behavior.  To be 
> specific, consider the following three cases
> 
> 
> case 1
>           use, intrinsic :: iso_c_binding
>           implicit none
>           integer stat
>           type(c_ptr) :: cptr
>           real(C_DOUBLE), pointer :: p, q
>           allocate(p)
>           cptr = c_loc(p)
>           call c_f_pointer(cptr, q)
>           deallocate(q, stat=stat)
>           print *, stat
>           end
> 

This one looks OK.

> case 2
>           use, intrinsic :: iso_c_binding
>           implicit none
>           integer stat
>           type(c_ptr) :: cptr
>           real(C_DOUBLE), pointer :: p, q
>           real(C_DOUBLE), target :: t
>           p => t
>           cptr = c_loc(p)
>           call c_f_pointer(cptr, q)
>           deallocate(q, stat=stat)
>           print *, stat
>           end
> 

This is no legal. See 6.7.3.3 'Deallocation of pointer targets'.  In 
particular "Deallocating a pointer ... whose target was not created by 
an ALLOCATE statement causes an error condition in the DEALLOCATE 
statement".


> case 3
>           use, intrinsic :: iso_c_binding
>           implicit none
>           integer stat
>           type(c_ptr) :: cptr
>           real(C_DOUBLE), pointer :: p, q
>           real(C_DOUBLE), target, allocatable :: t
>           allocate (t)
>           p => t
>           cptr = c_loc(p)
>           call c_f_pointer(cptr, q)
>           deallocate(q, stat=stat)
>           print *, stat
>           end
> 

Same section.  "If a pointer is associated with an allocatable entity, 
the pointer shall not be deallocated."

Cheers,
Bill


> 
> Note these tests are simplified with all pointers being entities 
> declared in main.  In practice these pointers could be dummy pointers 
> and the deallocations can happen in different scopes from the 
> c_f_pointer() function call.
> 
> The question is: does standard require specific behavior on each of the 
> DEALLOCATE statement?  I found the standard is vague in this.
> 
> Cheers,
> 
> 
> Jim Xia
> 
> XL Fortran Compiler Test
> IBM Toronto Lab at 8200 Warden Ave, Markham, On, L6G 1C7
> Phone (905) 413-3444  Tie-line 313-3444
> email: jimxia at ca.ibm.com
> D2/YF7/8200 /MKM
> 
> http://www.ibm.com/software/awdtools/fortran/xlfortran
> 

-- 
Bill Long                                           longb at cray.com
Fortran Technical Support    &                 voice: 651-605-9024
Bioinformatics Software Development            fax:   651-605-9142
Cray Inc./Cray Plaza, Suite 210/380 Jackson St./St. Paul, MN 55101





More information about the J3 mailing list