What does "array cannot have a deferred shape" mean in fortran?

12.4k views Asked by At

I have a simple fortran function that computes the Kronecker product:

function kron(A, B)
    implicit none
    real, intent(in) :: A(:, :), B(:, :)
    integer :: i, j, ma, na, mb, nb
    real, dimension(:, :) :: kron

    ma = ubound(A, 1)
    na = ubound(A, 2)
    mb = ubound(b, 1)
    nb = ubound(b, 2)
    forall(i=1:ma, j=1:na)
        kron(mb*(i-1)+1:mb*i, nb*(j-1)+1:nb*j) = A(i,j)*B
    end forall
end function kron

It's inside a module, but when I compile it with gfortran -static -ffree-form -std=f2003 -Wall, I get these errors:

function kron(A, B)
                1
Error: Array 'kron' at (1) cannot have a deferred shape

Is this error occurring because you're supposed to know the size of the array to be returned beforehand?

3

There are 3 answers

6
Kyle Kanos On BEST ANSWER

That is exactly what the error is telling you: kron must have an explicit shape. If you do not want to pass the array sizes beforehand, you'd have to define kron as

real, dimension(lbound(a,dim=1):ubound(a,dim=1),&
                lbound(a,dim=2):ubound(a,dim=2)) :: kron

Using this particular explicit declaration above does compile for me on gfortran 4.6.3.

1
Navyanth Kusam On

A deferred-shape array that has the ALLOCATABLE attribute is referred to as an allocatable array. Its bounds and shape are determined when storage is allocated for it by an ALLOCATE statement.

try this

real, intent(in), allocatable, dimension(:, :: A(:, :), B(:, :)

1
Adrian Barbuio On

You just need to define the allocatable array as allocatable, i.e replace the kron definition with;

real, allocatable, dimension(:,:) :: kron

This also compiles fine in 4.6.3 and is defined at: https://docs.roguewave.com/codedynamics/2017.0/html/index.html#page/TotalViewLH/totalviewlhug-examining-data.09.10.html

Hopefully this should save you some effort, especially considering there is no need to define a lower bound here!