(j3.2006) CONSTANT POINTER
Daniel C Chen
cdchen
Wed Oct 12 20:30:23 EDT 2016
Hi all,
Andres Vidal requested a new CONST attribute for pointers to preventing
modifying private module variables.
Consider the following test case:
module m
? ? integer, private, target :: a(100,100,100)
contains
? ? subroutine GetA(arg)
? ? ? ? Integer, pointer :: arg(:,:,:)
? ? ? ? arg=>a
? ? end subroutine GetA
end module m
It declares array 'a' private because it doesn't want 'a' to be accessible
from outside (to avoid names conflict).
In the module procedure GetA(), it sets the pointer 'arg' to point to 'a'
since from outside the module the shape of 'a' is not known.
However, 'a' then can be modified from the outside.
To fix it, a constant pointer can be used as:
module m
? ? integer, private, target :: a(100,100,100)
contains
? ? subroutine GetA(arg)
? ? ? ? Integer, pointer, const :: arg(:,:,:)
? ? ? ? arg=>a
? ? end subroutine GetA
end module m
Use m
Integer, pointer, const :: ptr
Call GetA(ptr)
End
The idea is that a pointer with the CONST attribute
1. cannot change the value of the target it associates to.
2. If it appears as the RHS of a pointer assignment, the LHS must also have
the CONST attribute.
3. If it is passed as an actual argument, the corresponding pointer dummy
argument must also have the CONST attribute.
Is this a desired new feature? Any comments are welcome.
Thanks,
Daniel
XL Fortran Development - IBM Toronto Software Lab
Phone: 905-413-3056
Tie: 969-3056
Email: cdchen at ca.ibm.com
http://www.ibm.com/software/awdtools/fortran/xlfortran
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.j3-fortran.org/pipermail/j3/attachments/20161012/79669f89/attachment.html
More information about the J3
mailing list