Passing pointer to subroutines(functions)

When passing a pointer array to subroutines(functions)

tell me if there’s any difference between

  1. CALL SAMPLE_ROUTINE(A)

and

  1. CALL SAMPLE_ROUTINE(A(:))

What I encountered is an interface problem. If I declare A as pointer in the interface block, 2 causes access violation error, while 1 works properly.

If those two are different, isn’t the compiler too inflexible to subtle differences?

If you are getting a runtime access violation, it probably depends on how you actually have coded SAMPLE_ROUTINE (how did you declare the dummy argument). Can you include that code? Also, include the actual interfaces you’ve tried.

The original code is too big; it’s a commercial code.

So, I will post two simple examples.

The problem occuring here is just the same to that occuring in my code.

PROGRAM TEST
    IMPLICIT NONE
    REAL, POINTER :: A(:, :)
    
    INTERFACE
    SUBROUTINE ROUTINE(A)
    IMPLICIT NONE
    REAL, POINTER :: A(:, :)
    END SUBROUTINE
    END INTERFACE
    
    ALLOCATE(A(100, 100))
    A(50, 50) = 50
    CALL ROUTINE(A)        !working
    CALL ROUTINE(A(:, :))  !access violation
    END PROGRAM
    
    SUBROUTINE ROUTINE(A)
    IMPLICIT NONE
    REAL, POINTER :: A(:, :)
    WRITE(*, *), A(50, 50)
    END SUBROUTINE



PROGRAM TEST
    IMPLICIT NONE
    REAL, POINTER :: A(:, :)
    
    INTERFACE
    SUBROUTINE ROUTINE(A)
    IMPLICIT NONE
    REAL :: A(:, :)
    END SUBROUTINE
    END INTERFACE
    
    ALLOCATE(A(100, 100))
    A(50, 50) = 50
    CALL ROUTINE(A)        !access violation
    CALL ROUTINE(A(:, :))  !working
    END PROGRAM
    
    SUBROUTINE ROUTINE(A)
    IMPLICIT NONE
    REAL :: A(:, :)
    WRITE(*, *), A(50, 50)
    END SUBROUTINE

Is this normal?

Hi CNJ,

You code is illegal. When you pass in “A(:,:)” you are passing in an array section which will loose it’s pointer property.

When compiling with PGI 15.5, you’ll get a syntax error:

% pgfortran -V15.5 testptr.f90
PGF90-S-0188-Argument number 1 (non-POINTER) to routine: type mismatch (testptr.f90: 15)
0 inform, 0 warnings, 1 severes, 0 fatal for test

In the F2008 standard this limitation was loosened a bit where target arrays can be passed in. From 12.5.2.7 Pointer dummy variables of the F2008 standard:

If the dummy argument does not have the INTENT (IN), the actual argument shall be a pointer. Otherwise, the
actual argument shall be a pointer or a valid target for the dummy pointer in a pointer assignment statement. If
the actual argument is not a pointer, the dummy pointer becomes pointer associated with the actual argument.

Of course with regard to “valid target”, this is from the specification as well:

5.3.17 TARGET attribute

The TARGET attribute specifes that a data object may have a pointer associated with it (7.2.2). An object
without the TARGET attribute shall not have a pointer associated with it.

Although A is an array, A(:, : ) is an array section, but it does not have the target nor pointer attribute.

So the problem looks like that when we added this new F2008 support in the 15.7, we lost emitting the syntax error. I added TPR#22290 requesting that the error message be added back in this case.

Hope this helps,
Mat

Thanks.

I hope the problem is fixed soon.

It is quite tough to find those errors one by one during runtime.

TPR 22290 - UF: User code not giving error message when passing sub-array to pointer dummy arg
has been fixed in the 16.4 release