Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Assignment changes array bounds

Svein-Atle_Engeseth
407 Views
Hi,
I have a program usingtwo allocatable arrays, A and B,both allocated like A(0:N), N = 10.
After the assignment
A = A + B
the bounds of A are 1 and (N+1).
The compiler switch is /assume:realloc_lhs.
Is this according to the standard, when no reallocation is necessary?
If so, does changing the assignment to
A(0:) = A + B
inflict any penalties? ( Apart from not looking good )

Compiler IVF 11.1.051

Thanks
Svein-Atle
0 Kudos
1 Reply
Steven_L_Intel1
Employee
407 Views
The compiler is correct according to the standard, though finding the words that say so can be difficult for one not used to the standard.

First, the standard says, in discussion of expressions, that the shape of A+B is the same as the shape of the operands - other wording says that the shape of the operands must be the same. But "shape" is merely the extent (number of elements) of each dimension, not the same lower and upper bounds.

Consider if A had been 0:9 and B 10:19. Both have the same shape, [10], but obviously different bounds. If the bounds were taken from the operands, what would you want the new bounds to be?

Where you have to look is at the definition of the LBOUND and UBOUND intrinsics. Here it says that if the argument is a whole array (with some exceptions), then the result is the lower bound of the subscript, otherwise it is 1. Similarly for UBOUND, if the argument is not a whole array, the result is the extent of the argument. Therefore, 1:10 is the correct bounds for A+B and also correct for the new A.

If you say A(:) then you won't get the reallocation - no need to say A(0:).

I will comment that 11.1 had some errors in how it assigned the bounds of the left side of some array assignments when reallocation was done - these were fixed in 12.0. But the behavior you see is correct.
0 Kudos
Reply