问题描述
这两种声明假定大小数组的方法有什么区别吗?
Is there any difference between these two methods of declaring an assumed-size array?
例如
real, dimension(:) :: arr
和
real :: arr(*)
推荐答案
表格
real, dimension(:) :: arr
声明一个assumed-shape数组,而表单
real :: arr(*)
声明一个 假定大小 数组.
而且,是的,它们的用途之间存在差异.之所以出现差异,是因为编译器大致知道"假定形状数组的形状,但不知道假定大小数组的形状.编译器可用的额外信息意味着,除其他外,假定形状的数组可用于整个数组表达式.假定大小的数组只能在不需要数组形状的过程引用中的实际参数时用于整个数组表达式.哦,还有对内在 lbound
的调用——但不是在对内在 ubound
的调用中.仔细阅读标准或一本好的 Fortran 书籍,您会发现其他一些微妙且不那么微妙的差异.
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.
对于新 Fortran 程序员的一些建议是尽可能使用假定形状的数组.它们在 Fortran 90 之前不可用,因此您会在旧代码中看到许多假定大小的数组.假设形状数组在新代码中更好,因为 shape
和 size
函数可用于查询它们的大小以避免越界错误和 分配维度取决于输入数组维度的数组
.
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(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!