保留内在分配中的分配界限 [英] Preserve bounds in allocation in intrinsic assignment
问题描述
我在分配时使用自动分配来计算两个数组的差异,边界从 0 开始:
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
gfortran 和 ifort 都给出输出:
Both, gfortran and ifort give the output:
0
0
1
为什么 c 的边界与 a 和 b 不同?有没有一种简洁(没有明确分配)的方法来确保 c 具有相同的界限?
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?
推荐答案
为什么 c 和 a und b 的边界不一样?
因为它应该被分配到与 a
和 b
相同的形状并且从 1 开始.(无论如何 ba
是一个表达式,它也是从 1 开始的,但不重要.如果 b
和 a
从不同的索引开始呢?)
Because it shall be allocated to the same shape as a
and b
and starting at 1. (Anyway b-a
is an expression and it starts at 1 as well, but it is not important. What if b
and a
start at different indexes?)
有没有一种简洁(没有明确分配)的方法来确保 c 具有相同的边界?
没有.但您至少可以使用 mold=a
或 source=a
显式分配.
No. But you can at least explicitly allocate with mold=a
or source=a
.
这篇关于保留内在分配中的分配界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!