Jonatan Öström
Jonatan Öström

Reputation: 2609

Warning for explicit-shape array reshaping

Let all routines be inside modules.

  1. If I pass the array real*8 aa(5,3) to a routine

    subroutine sub(bb)
    real*8, intent(in) :: bb(2,5)
    ...
    end subroutine
    

    with the statement call sub(aa) this will compile without a warning and the first 2 columns of aa will fill the bb array. The elements of the arrays aa and bb are aligned very differently.

  2. If instead the routine is written

    subroutine sub(bb)
    real*8, intent(in) :: bb(:,:)
    ...
    end subroutine
    

    then bb would have the same shape and storage order as aa.

Q: The first behavior is quite dangerous if one forgets that there are explicit-size declarations in a routine. Can I make the compiler warn when explicit-shape arrays change shape/alignment?

Upvotes: 4

Views: 104

Answers (1)

I am not aware of a compiler option to warn about this as it is a perfectly legitimate practise using the storage association - we have several questions and answers about this concept. It can be quite useful.

Upvotes: 1

Related Questions