(j3.2006) CONSTANT POINTER

Van Snyder van.snyder
Wed Oct 12 21:17:12 EDT 2016


This is the way INTENT(IN) and PROTECTED should have worked.

On Wed, 2016-10-12 at 20:30 -0400, Daniel C Chen wrote:
> 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
> 
> 
> _______________________________________________
> J3 mailing list
> J3 at mailman.j3-fortran.org
> http://mailman.j3-fortran.org/mailman/listinfo/j3





More information about the J3 mailing list