I have come across what appears to be a compiler bug.
I have a very simple program that demonstrates this. I want to point a 3D array at an equivalent sized 1D array
here is the code
MODULE array_1D
integer, allocatable, target, save :: array(:)
end Module array_1D
C-----------------------------------------------------
PROGRAM main
call alloc
call write1
call write2
call sub
call write1
call write2
end
C-----------------------------------------------------
Subroutine alloc
use array_1D
l=2
n=3
m=4
allocate(array(l*n*m))
array =0
return
end
C-----------------------------------------------------
Subroutine sub
use array_1D
l=2
n=3
m=4
do i=1,l*n*m
array(i)=i
end do
return
end
C-----------------------------------------------------
Subroutine write1
use array_1D
l=2
n=3
m=4
write(*,'(24I4)')(array(i),i=1,l*n*m)
return
end
C-----------------------------------------------------
Subroutine write2
use array_1D
integer, pointer :: array3d(:,:,:)
l=2
n=3
m=4
array3d(1:l,1:n,1:m)=>array
write(*,'(24I4)')(((array3d(i,j,k),i=1,l),j=1,n),k=1,m)
return
end
when I compile this I don’t get any compile time errors; however when I run it I get the following results.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
1 2 3 4 5 6 6 7 8 9 10 11 11 12 13 14 15 16 16 17 18 19 20 21
As you can see it appears that the compiler is miscalculating the offset in the memory for the third dimension. I get similar results with different ranges of l,n,m. I got the same results using PGF90 or PGFORTRAN
I compiled the exact same source code using another brand of compiler and got the expected results:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Is there something wrong with the compiler or am I missing something here?