I am using automatic allocation on assignment to calculate the difference of two arrays, with bounds starting at 0:
program main
implicit none
integer, allocatable :: a(:), b(:), c(:)
allocate(a(0:10))
allocate(b(0:10))
a = 1
b = 2
write (*,*) lbound(a)
write (*,*) lbound(b)
c = b - a
write (*,*) lbound(c)
end program main
Both, gfortran and ifort give the output:
0
0
1
Why doesn't c have the same bounds as a and b? Is there a short and concise (without an explicit allocate) way of making sure c has the same bounds?
Why doesn't c have the same bounds as a und b?
Because it shall be allocated to the same shape as a
and b
and starting at the lower bound of the expression on the right hand side. The b-a
is an expression and it starts at 1 and any result f array arithmetic operations will start like that. What if b
and a
start at different indexes?)
Is there a short and concise (without an explicit allocate) way of making sure c has the same bounds?
No. But you can at least explicitly allocate with mold=a
or source=a
.