Quantcast
Channel: Recent posts
Viewing all articles
Browse latest Browse all 415

Incorrect reallocation of the LHS on assignment

$
0
0

The attached code prints

$ ./test
 Before:            2
 After:            1 ; w =  0.6000000    

which is incorrect, since the shape of w should not change during the
assignment (no need for reallocation, and in any case the rhs has two
elements).

Notice that using w(:) solves the problem.

Compiled with
$ ifort test.f90 -o test
$ ifort -V
Intel(R) Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 17.0.1.132 Build 20161005

program p
 implicit none

 integer, allocatable :: ii(:)
 real,    allocatable :: w(:), zz(:,:)

  allocate( ii , source=(/2,1/) )
  allocate( zz(2,2) , w(2) )

  zz(:,1) = 0.3
  zz(:,2) = 0.6

  write(*,*) "Before: ", shape(w)
  w = zz(1,ii)
  !w(:) = zz(1,ii) ! no problems here
  write(*,*) "After: ", shape(w), "; w =", w

end program p

 


Viewing all articles
Browse latest Browse all 415

Trending Articles