https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103434

            Bug ID: 103434
           Summary: Pointer subobject does not show to correct memory
                    location
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: baradi09 at gmail dot com
  Target Milestone: ---

Based on the discussion on FD
(https://fortran-lang.discourse.group/t/is-the-section-of-a-pointer-to-an-array-a-valid-pointer/2331),
I'd assume, that the following code is standard conforming. However, the result
with gfortran seems to be incorrect.

*** Code:

module test
  implicit none

  type :: pointer_wrapper
    real, pointer :: ptr(:) => null()
  end type pointer_wrapper

contains

  subroutine store_pointer(wrapper, ptr)
    type(pointer_wrapper), intent(out) :: wrapper
    real, pointer, intent(in) :: ptr(:)
    wrapper%ptr => ptr
  end subroutine store_pointer


  subroutine use_pointer(wrapper)
    type(pointer_wrapper), intent(inout) :: wrapper
    wrapper%ptr(:) = wrapper%ptr + 1.0
  end subroutine use_pointer

end module test


program testprog
  use test
  implicit none

  real, allocatable, target :: data(:,:)
  real, pointer :: ptr(:,:)

  type(pointer_wrapper) :: wrapper
  integer :: ii

  allocate(data(4, 2))
  ptr => data(:,:)
  data(:,:) = 0.0
  do ii = 1, size(data, dim=2)
    print *, "#", ii
    print *, "BEFORE ", ii, maxval(ptr(:,ii))
    call store_pointer(wrapper, ptr(:,ii))
    print *, "BETWEEN", ii, maxval(ptr(:,ii))
    call use_pointer(wrapper)
    print *, "AFTER  ", ii, maxval(ptr(:,ii))
  end do

end program testprog

*** Output:

 #           1
 BEFORE            1   0.00000000    
 BETWEEN           1   0.00000000    
 AFTER             1   1.00000000    
 #           2
 BEFORE            2   1.00000000    
 BETWEEN           2   1.00000000    
 AFTER             2   1.00000000    

*** Expected output:

 #           1
 BEFORE            1   0.00000000    
 BETWEEN           1   0.00000000    
 AFTER             1   1.00000000    
 #           2
 BEFORE            2   0.00000000    
 BETWEEN           2   0.00000000    
 AFTER             2   1.00000000    

It seems, as if store_pointer would point to a memory location larger as it
should be, so that also data outside of the actual stride is modified. Intel
and NAG deliver the expected output.

Reply via email to