(j3.2006) C_LOC and deallocating a pointer

Cohen Malcolm malcolm
Thu Jun 16 21:47:08 EDT 2016


Case 1 is valid if the allocation was via the ALLOCATE statement.  If it was 
not, the program is not standard-conforming, and there is no requirement to 
raise an error.  OTOH there are multitudinous implementation techniques 
which can detect this error should that be considered desirable.

Case 2 is not valid.  There is no requirement to detect this error. 
Nonetheless, it is detectable.

Case 3 is also not valid.  Again, there is no requirement to detect this 
error.  Again, the error is detectable and there are implementations which 
can detect it.  There is no significant difference between this situation 
and one arising without the use of C_LOC or interoperability.

The rules are simple:
(1) if you allocate it via Fortran, you must deallocate it via Fortran;
(2) if you allocate it via C, you must deallocate it via C;
(3) if it is a Fortran ALLOCATABLE you must not deallocate it except via the 
ALLOCATABLE variable.

And it is not required to detect violations of any of these rules. 
Nonetheless, it is not hard to do so should that be felt desirable.

Cheers,

-----Original Message----- 
From: Bill Long
Sent: Friday, June 17, 2016 6:21 AM
To: fortran standards email list for J3
Subject: (j3.2006) C_LOC and deallocating a pointer


In general, if you try to deallocate a pointer, the memory involved should 
have been allocated by allocation of a pointer.   Thus, for example, if the 
target of the pointer is a explicit-shape array or is allocatable, there is 
an error. That is all pretty clear if you stay in the Fortran domain. 
However, what about cases like this:


type(C_PTR) :: px
integer,pointer :: p(:), q(:), r(:)
?
! Part A - somehow allocate memory for p
?
q => p
px = C_LOC(q)

! Part B - Pass px to a C function that uses it somehow

call c_f_pointer (px, r(100))
deallocate(r)

========

Case 1:  Is this ever legal, independent of how P was allocated in Part A? 
The variable px contains only the C address of the target - all the Fortran 
?pointer? information is lost (with typical implementations) at this point. 
So the deallocation of r does not have sufficient information to determine 
whether the deallocate should generate an error.

Case 2: In part A the memory is allocated by malloc() in a C function. How 
would the deallocate know that the C allocation was for 100 elements?

Case 3: The declaration for P is, instead, allocatable,target, and the 
memory in Part A is allocated by a Fortran ALLOCATE statement. In this case 
the runtime should raise an error, but is there sufficient information to 
determine that?  Unfortunately, compilers are not in agreement about this 
case.

Cheers,
Bill





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


_______________________________________________
J3 mailing list
J3 at mailman.j3-fortran.org
http://mailman.j3-fortran.org/mailman/listinfo/j3

________________________________________________________________________
This e-mail has been scanned for all viruses by Star.
________________________________________________________________________

-- 
........................Malcolm Cohen, Nihon NAG, Tokyo. 




More information about the J3 mailing list