0

My code is currently breaking when I try to pass an array to another array. I'm using gfortran as the compiler with no special flags. I'm also writing the file as a ".f95". Here's the backbone of the code that's causing the problem:

    program foo
        integer, parameter :: pp = 16

    call fooyoo

    contains 

    subroutine fooyoo

        real(kind=pp), allocatable :: f(:,:,:), y_ix(:), r(:)
        integer :: ii, jj, nn


        nn = 32
        r = zoo(nn)
        y_ix = r    ! this is done because I'm actually allocating two other 1D arrays

    do ii = 1, nn
        do jj = 1, nn
            write(*,*) "crashes here"            
                f(ii,jj,:) = y_ix
            write(*,*) "nothing here"
        end do
    end do

    end subroutine

    function zoo(nn) result(r)
        integer :: i, nn
        real(kind=pp) :: r(nn)

    do i = 1, nn
        r(i) = i
    end do

    end function
    end program

1 Answer 1

3

Probably

do ii = 1, nn
    do jj = 1, nn
        write(*,*) "crashes on executing the next line because programmer forgot ", &
                   "to allocate f before trying to set values"            
            f(ii,jj,:) = y_ix
        write(*,*) "nothing here"
    end do
end do

In this line

    y_ix = r

automatic allocation works because the expression on the lhs is an allocatable array. In

    f(ii,jj,:) = y_ix

automatic allocation fails because the expression on the lhs is an array section, not an allocatable array.

Sign up to request clarification or add additional context in comments.

2 Comments

I did this: IF (.NOT. ALLOCATED(f)) ALLOCATE(f(nn,nn,nn)) seems to have worked, but seems inelegant.
I don't understand why you bothered to test whether f was allocated. From the code you have shown there is no possibility that it could be. But whatever floats your boat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.