fortran pointer array to multiple targets -
is there way have in fortran pointer array pointing different targets?
the following code shows trying do, not work want since second association overwrites first
implicit none integer, parameter :: n = 3 integer, target :: a(n), b(n) integer, pointer :: c(:) => null() = 4 b = 5 c(1:n) => a(1:n) c(n+1:2*n) => b(1:n) c(1:2*n) = 1 print*, print*, b output (ifort) 4 1 1 1 1 1 output (gfortran) 4 4 4 1 1 1
and, idea why ifort , gfortran behaves different in case?
no, there no simple way this, @ least far can see. maybe if why trying might come suitable answer, instance in mind derived types and/or array constructors might way go without context difficult say.
as why different answers accessed array out of bounds can happen:
ian@ian-pc:~/test/stack$ cat point.f90 program point implicit none integer, parameter :: n = 3 integer, target :: a(n), b(n) integer, pointer :: c(:) => null() = 4 b = 5 c(1:n) => a(1:n) c(n+1:2*n) => b(1:n) c(1:2*n) = 1 print*, print*, b end program point ian@ian-pc:~/test/stack$ nagfor -c=all -c=undefined point.f90 nag fortran compiler release 5.3.1(907) [nag fortran compiler normal termination] ian@ian-pc:~/test/stack$ ./a.out runtime error: point.f90, line 14: subscript 1 of c (value 1) out of range (4:6) program terminated fatal error aborted (core dumped)
think lines
c(1:n) => a(1:n) c(n+1:2*n) => b(1:n)
are doing. first line says forget whatever c before, c( 1:n ) alias a( 1:n ). note allowed indices c run 1 n. second line throws away reference , says c( n+1:2*n) alias b( 1:n ). note indices of c run n+1:2*n, while b run 1:n, fine both have n elements , matters. next line says
c(1:2*n) = 1
which can not correct have said lowest allowed index c n+1, , n=3 1 not valid index. out of bounds , can happen.
i suggest when developing use debugging options on compiler - in experience can save hours avoiding kind of thing!
Comments
Post a Comment