Kenneth_Smith
Joined: 18 May 2012 Posts: 710 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Mon May 15, 2023 12:21 pm Post subject: Protected attribute + allocatable arrays |
|
|
The recently introduced PROTECTED attribute for module variables works correctly for STATIC variables of both INTRINSIC and USER DEFINED types.
If the PROTECTED variable is an ALLOCATABLE array of either INTRINSIC or USER DEFINED type, the array is not protected.
I have found that it is possible to ALLOCATE such an array, and change the contents of the array via a procedure which is not CONTAINED within the module which hosts the array.
Below are two examples which demonstrate these issues.
Code: | module a_mod
integer, parameter :: imax = 5
real, protected, allocatable :: a(:)
end module a_mod
program main
use a_mod
allocate(a(1:imax)) ! ### Should not be permitted.
a = 1.0 ! ### Should not be permitted.
print*, a
end program main
|
Code: | module a_mod
integer, parameter :: imax = 5
type i3e ; real :: amps ; end type i3e
type(i3e), protected, allocatable :: a(:)
contains
subroutine init_array
allocate(a(1:imax)) ; a%amps = 1.0
end subroutine init_array
end module a_mod
program main
use a_mod
call init_array
print*,a%amps
a%amps = 1000.d0 ! ### Should not be permitted.
print*,a%amps
end program main
|
Not urgent. One to look at when time permits. |
|