Silverfrost Forums

Welcome to our forums

SOURCE= clause in ALLOCATE statement

29 Dec 2025 12:49 #32614

Fortran 2003 introduced sourced allocation, as in:

ALLOCATE(v(1:4), SOURCE=2.3)

Fortran 2008 enhanced this with more features. The source clause is a convenient way of initialising an array or other variable immediately after allocation. FTN95 does not accept this version of ALLOCATE.

29 Dec 2025 8:03 #32615

mecej4

Thanks for the feedback. This feature was included in v9.10.

29 Dec 2025 1:58 #32617

Paul, I tried the compiler on the test code:

subroutine sub(x)
real,allocatable,intent(out) :: x(:)
allocate(x(1:10),source=2.0)
return
end subroutine

and the compiler responded:

[FTN95/Win32 Ver. 9.10.0.0 Copyright (c) Silverfrost Ltd 1993-2025]
ERROR S:\Arena\sub.F90 3:  INTEGER expression expected in array bounds subscript
    1 ERROR  [<SUB> FTN95 v9.10.0.0]
*** Compilation failed
29 Dec 2025 4:06 #32631

mecej4

Thanks for the reply. I now see that it does not work when x is an argument.

29 Dec 2025 4:43 #32632

Paul, Not only is x an argument, it is also allocatable. Does that affect how the compiler views the code?

29 Dec 2025 6:06 #32633

mecej4

Yes, but I would have expected the compiler to be able to handle this.

29 Dec 2025 9:55 #32635

Here is another example code that FTN 95 rejects as being incorrect.

module allow_mod
   implicit none
   private
   public :: allow_t
   type level_t
      integer :: nval
      integer, allocatable :: val(:),map(:)
   end type level_t
   type allow_t
      integer                    :: N, i5modNx = -1
      type(level_t)              :: lt
      type(level_t), allocatable :: l(:),k(:)
   end type allow_t
end module allow_mod

subroutine set_allow( modvals, allow )
  use allow_mod, only: allow_t
  implicit none
    integer, intent(in)                     :: modvals(:)
    type(allow_t), intent(out), allocatable :: allow(:)
    integer :: i,j,jx,N, maxN
    logical :: mask(10) = .true.
      allocate( allow(size(modvals)) )
      maxN = maxval( allow(:)%N )
      do i = 1, size(modvals)
         N = allow(i)%N
         do j = 1, maxN
            jx = mod(j,maxN)
            mask(jx) = mask(jx) .and. (allow(i)%lt%map( mod(j,N) )/=0)
            print *,i,j,jx,mask(jx)
         enddo
      enddo
    return
   end subroutine set_allow

The compiler says

ERROR S:\euler\bug.F90 29:  map is not a component of LEVEL_T
ERROR S:\euler\bug.F90 29:  Compilation abandoned
30 Dec 2025 8:31 #32636

mecej4

Thank you for the feedback. I have logged this for investigation.

31 Dec 2025 8:59 #32640

For the next release FTN95 has been extended so that it can handle the code with 'allocate(x(1:10),source=2.0)' posted here on 29 December.

31 Dec 2025 9:48 #32641

mecej4

The problem for FTN95 in the module allow_mod is that the TYPE level_t is declared as PRIVATE. It compiles if level_t is declared as PUBLIC.

I think that FTN95 ought to accept PRIVATE in this context but it might be very difficult to fix.

Hopefully the code will run when ammended in this way but I have not tested it.

31 Dec 2025 1:56 #32643

Paul, I tried adding

PUBLIC:: allow_t, level_t

in module allow_mod, but that did not keep the compiler from saying that 'map is not a component of LEVEL_T.' Thanks. I can get along after making everything public

31 Dec 2025 2:17 #32644

mecej4

I compiles for me when that change is made.

This has now been fixed for the next release of FTN95 so that LEVEL_T will become PUBLIC when ALLOW_T is public and the default is PRIVATE. (Not as difficult as I feared.)

Please login to reply.