问题描述
是否有声明假定大小的数组的这两种方法有什么区别?
例如。
真实的,尺寸(:) :: ARR
和
真正:: ARR(*)
形式
真实的,尺寸(:) :: ARR
声明了一个假定形状
阵列,而形式
真正:: ARR(*)
声明了一个假定大小
阵列。
和,是的,有使用它们之间的差异。的差异的产生是因为,大约,编译器知道的假定形状阵列的形状,但不是假定大小阵列。提供给编译器的额外信息是指,除其他事项外,假定形状阵列可在整个阵列的前pressions使用。假定大小的数组只能在整个阵列前pressions时使用它在不需要数组的形状的过程引用的实际参数。哦,还有到内在 LBOUND
通话 - 在呼叫而不是内在 UBOUND
。还有其他一些微妙的,不那么微妙,差异,你的标准还是不错的Fortran书细读就会发现。
的几点建议对新的Fortran编程是可能时使用假定形状数组。他们不是用Fortran 90的前,所以你会看到很多老code假定大小数组。假定形状阵列是在新的code更好,因为形状和大小的功能可以用来查询它们的大小,以避免外的边界错误并ALLOCATE阵列,其尺寸取决于输入阵列的尺寸。
Is there any difference between these two methods of declaring an assumed-size array?
e.g.
real, dimension(:) :: arr
and
real :: arr(*)
The form
real, dimension(:) :: arr
declares an assumed-shape
array, while the form
real :: arr(*)
declares an assumed-size
array.
And, yes, there are differences between their use. The differences arise because, approximately, the compiler 'knows' the shape of the assumed-shape array but not of the assumed-size array. The extra information available to the compiler means that, among other things, assumed-shape arrays can be used in whole-array expressions. An assumed-size array can only be used in whole array expressions when it's an actual argument in a procedure reference that does not require the array's shape. Oh, and also in a call to the intrinsic lbound
-- but not in a call to the intrinsic ubound
. There are other subtle, and not-so-subtle, differences which your close reading of the standard or of a good Fortran book will reveal.
Some advice for new Fortran programmers is to use assumed-shape arrays when possible. They were not available before Fortran 90, so you will see lots of assumed-size arrays in old code. Assumed-shape arrays are better in new code, because the SHAPE and SIZE functions can be used to query their sizes to avoid out-of-bounds error and to ALLOCATE arrays whose dimensions depend on the dimensions of input arrays.
这篇关于假定大小的数组:科隆主场迎战星号 - DIMENSION(:) ARR与ARR(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!