4

I'm trying to use an allocatable array in a subroutine but the compiler complains that

Error: Dummy argument 'locs' with INTENT(IN) in variable definition context (ALLOCATE object) at (1)

The only thing I could find was that I am supposed to use an explicit interface, which I am doing. Here the relevant code for the subroutine:

    RECURSIVE SUBROUTINE together(locs, LL, RL)

    INTEGER, DIMENSION(:,:), ALLOCATABLE, INTENT(IN)            :: locs
    INTEGER, INTENT(IN)                                         :: LL, RL


    ALLOCATE(locs(LL,RL))


END SUBROUTINE together

3 Answers 3

5

The compiler's error message is one descriptive of the problem. With INTENT(IN) you are saying that the object will not change, but you then go on to attempt to ALLOCATE it.

Yes, an explicit interface will be required for the calling, but that isn't the problem.

The Fortran 2008 standard says in section 5.3.10 that

A nonpointer object with the INTENT (IN) attribute shall not appear in a variable denition context

Allocation is one such context: section 16.6.7, point (11).

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

Comments

2

The locs dummy argument is allocatable, and has the INTENT(IN) attribute - the intent attribute here indicating that the calling procedure is providing information to the subroutine.

A consequence of the INTENT(IN) attribute is that you cannot change the allocation status (or value) of locs. Your ALLOCATE statement is attempting to do just that.

3 Comments

Thank you. That makes sense now! The reason I'm trying to do that is because I am trying to get this to work: IF (ANY(locs .eq. next(k:k+1))) THEN They don't have the same rank, I know that but I cannot think of a way around this without looping through the whole array
Try asking a separate question that details what you are trying to do.
Re the new question in the comment. Set a logical flag false. Set up a named loop containing an IF statement to check the condition. If the condition is satisfied, set the flag true and use exit to exit the loop early.
0

Try allocating your array in your main program, then when locs is pushed to your subroutine, use INTENT(INOUT) to tell the compiler you also want to change the contents of your array.

Comments

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.