Silverfrost Forums

Welcome to our forums

Protected attribute + allocatable arrays

15 May 2023 11:21 #30312

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.

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



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.

22 May 2023 8:29 #30342

Ken

Many thanks for the feedback. I have made a note that this needs fixing.

26 May 2023 10:00 #30361

These issues have now been fixed for the next release of FTN95.

Please login to reply.