通过时调整多维数组的大小 [英] Resizing of Multi-Dimensional Arrays when Passed

查看:22
本文介绍了通过时调整多维数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文中所有对数组的引用都是多维的.

All references to arrays in this post are multi-dimensional.

我知道当一个数组被传递给一个子程序时,它可以被声明为不同的维度/大小作为调用者.

I came to know that when an array is passed a subroutine, it can be declared with different dimensions/sizes as the caller.

作为一个具体的例子,BMAIN 在主程序中用 DIMENSION(6,5) 声明.BMAIN 作为 BSUB 传递给子程序,声明为:

As a specific example, BMAIN is declared with DIMENSION(6,5) in the main program. BMAIN is passed to a subroutine as BSUB, which is declared as:

INTEGER, INTENT(IN) :: BSUB(3,2,0:4)

问题:

BSUB 中的条目是否只是从 SBMAIN 一个一个地填写,直到被填写(按照此处解释的顺序 Fortran 中多维数组的线性索引)?

编译器是否执行了任何维度匹配?例如,如果 BSUB 被声明为 BSUB(0:4,3,2),它还会在正确的位置保存正确的条目吗?

Are there any dimension matching performed by the compiler? For instance, had BSUB been declared as BSUB(0:4,3,2), would it still hold the correct entries in the correct places?

推荐答案

来自 Fortran 2008 标准 (12.5.2.11.4):

From the Fortran 2008 standard (12.5.2.11.4):

表示元素序列的实际参数和对应于一个虚拟参数,它是一个数组是序列如果虚拟参数是显式形状或假定大小的数组.实际的等级和形状参数不需要与虚拟参数的等级和形状一致,但虚拟参数中的元素数量不得超过实际参数的元素序列中的元素数.如果虚拟参数是假定大小,元素的数量dummy 参数正是元素中的元素数量顺序.

An actual argument that represents an element sequence and corresponds to a dummy argument that is an array is sequence associated with the dummy argument if the dummy argument is an explicit-shape or assumed-size array. The rank and shape of the actual argument need not agree with the rank and shape of the dummy argument, but the number of elements in the dummy argument shall not exceed the number of elements in the element sequence of the actual argument. If the dummy argument is assumed-size, the number of elements in the dummy argument is exactly the number of elements in the element sequence.

在子程序中使用不同形状和等级的数组是完全合法的,只是不要在子程序中引用比数组实际拥有的更多的元素.临时数组可能是必要的,但通常不是.

It is completely legal to use a different shape and rank of the array in the subroutine, just do not reference more elements in the subroutine than the array actually has. Array temporary may be necessary, but often it is not.

这篇关于通过时调整多维数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆