[J3] Changing address of an allocatable on assignment when shapes match

Steve Lionel steve at stevelionel.com
Fri Aug 23 17:59:07 EDT 2019

a user asks when the storage location of an allocatable array can change 
during intrinsic assignment. I responded that if the shapes (or lengths, 
or in the case of polymorphic, kinds) don't match, the variable is 
deallocated and then reallocated to match the expression. But if 
everything matches, is it conforming to do a reallocation anyway?

According to the user, Sun/Oracle Fortran Studio 12.6 (this may not be a 
current version) does the reallocate even if the shapes match (but 
doesn't in assignment to S(:). I could imagine that an implementor might 
do this to avoid copying data twice in case of an overlap, but it feels 
wrong to me. Other compilers I tried don't reallocate under these 
conditions. Also, the user reports that adding the TARGET attribute 
disables the reallocation.

What are people's thoughts on this - is it ok by the standard, even if a 
bit weird?


program test
implicit none
real, dimension(:), allocatable :: S
integer :: i, idx(7) = [1,3,5,7,2,4,6]

do i=1,size(S)
   S(i) = i*i
end do

write(6,*) S
write(6,*) loc(S)

S = S(idx)

write(6,*) S
write(6,*) loc(S)

S(:) = S(idx)

write(6,*) S
write(6,*) loc(S)


end program

$ sunf90 -V
f90: Studio 12.6 Fortran 95 8.8 Linux_i386 2017/05/30

$ sunf90 test.f90 ; ./a.out
  1.0 4.0 9.0 16.0 25.0 36.0 49.0
  1.0 9.0 25.0 49.0 4.0 16.0 36.0
  1.0 25.0 4.0 36.0 9.0 49.0 16.0

More information about the J3 mailing list