Pointer dummy to target dummy

Why don’t you like something like:

module foo

contains

    subroutine foobar1(a)
    integer, intent(inout), target :: a(:)
    integer, pointer :: p(:)
    a(:) = 0
    call foobar2(a,p)
    a(:) = [1,2]
    print*, p   ! unreasonnable expectation: 1  2
    end subroutine

    subroutine foobar2(a,p)
    integer, intent(in), pointer :: a(:)
    integer, pointer, intent(out) :: p(:)
    p => a
    end subroutine

end module

program test
use foo

    integer :: a(4)
    call foobar1( a(::2) )

end
```

The pointer, intent(in) signals that the actual argument should have the target attribute and quite likely you are returning a pointer to a “subpart” of it.

I agree, this is probably what is happening. It is allowed for the compiler to do this, this is not a compiler error.

For the code to work “as expected”, the dummy argument declaration in foobar2() must allow strides in order to avoid the copy-in/copy-out argument association. There are several ways shown above to accomplish that. Also, the actual argument in the foobar2() reference must have the target attribute; this is another way that the programmer tells the compiler to not make local copies for the subroutine call.

If the pointer lives only within foobar1(), then there is no need to have a target declaration in the main program. If the pointer were also visible to the main program (e.g. if it were a module variable within module foo), then for it to be defined as expected in the main program, the array a(4) would also need the target attribute. However, in this last hypothetical case, I think most compilers would do the expected thing even without that target declaration, so that might be considered to be a latent bug in the code.

I have thought about a fun analogy for this problem (as with any analogy, it’s not perfect!):

  • John Ashape books an hotel room by internet, for 4 nights. His brother Joe Eshape will stay the first 2 nights, and he will stay the 2 other nights. But John forgets to mention that Joe is disabled.
  • Joe arrives at the hotel, and there’s no elevator. The hotel thus gives Joe the room 08, which is the only available one at the ground floor.
  • Suzy Pointer, the girlfriend of John, calls Joe by phone for whatever reason. Joe takes the opportunity to tell her that John will be there in room 08 in 2 days, in case she wants to come to see him.
  • Joe leaves the hotel. The room 08 was prebooked for someone else for the next 2 days, so the hotel prepares the room 15 at the first floor for John.
  • Suzy arrives and goes to see John at room 8, but finds instead a guy named Jordy Printer.
1 Like

My post was not about how to get a working code, but was a question about what in the standard was making this code illegal.

1 Like

I cannot see why you think the code is illegal. You have a side effect from the return of the values with P but the addresses should not work, so with IFX P(1) persists as 1, but you cannot print it, but you can use it, see picture. First column is f + p(1), second is print p(1). Granted this is weird.

What happens with other compilers, I only use Intel.

See there:

Is there similiar lines in the 2003 standard, I cannot see IFX getting to 2023 until 2040 or so?

But good answer.

Yeah, it’s there in 12.4.1.2