传递的数组具有子例程中期望的更多元素 [英] Passed array with more elements that expected in subroutine

查看:72
本文介绍了传递的数组具有子例程中期望的更多元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在共享库中有一个子例程:

I have a subroutine in a shared library:

SUBROUTINE DLLSUBR(ARR)
   IMPLICIT NONE
   INTEGER, PARAMETER :: N = 2
   REAL ARR(0:N)
   arr(0) = 0
   arr(1) = 1
   arr(2) = 2
END

让我们假设我将通过以下方式从可执行文件中调用它:

And let's assume I will call it from executable by:

REAL ARR(0:3)
CALL DLLSUBR(ARR)

注意:该代码可以愉快地编译并运行(DLLSUBR在模块内部),而在Debug +

Note: The code happily compiles and runs (DLLSUBR is inside a module) without any warning or error in Debug + /check:all option switched on.

这会导致内存损坏或某些奇怪的行为吗?在哪里可以找到有关在Fortran规范中传递具有不同大小的数组的信息?

Could this lead to memory corruption or some weird behaviour? Where I can find info about passing array with different size in the Fortran specification?

推荐答案

如果您使虚拟参数元素计数小于或等于,则序列关联规则实际上允许使用显式形状数组.当子例程需要更多的元素时,它将被禁止.

It is actually allowed for explicit shape arrays by the rules of sequence association, if you make the dummy argument element count to be smaller or equal. It is prohibited when the subroutine expects more elements then it gets.

显式形状数组通常要求参数由副本传递.当编译器无法证明数组是连续的(指针或假定的形状数组虚拟参数)时,就会发生这种情况.如果传递的元素数量较少,则子例程可以在复制数组部分之后访问一些垃圾.

The explicit shape arrays often require the arguments to be passed by a copy. This happens when the compiler cannot prove the array is contiguous (a pointer or an assumed shape array dummy argument). If smaller number of elements was passed, the subroutine could then access some garbage after the copy of the portion of the array.

在您的情况下,一切都会好的,因为您将更多的内容传递给期望更少的子例程.

In your case everything will be OK, because you are passing more to a subroutine expecting less.

Fortran 2008 12.5.2.11.4:

Fortran 2008 12.5.2.11.4:

4一个实际的参数,它表示元素序列和对应于一个虚拟参数,它是一个数组,是一个序列如果哑元参数是显形或假定大小的数组.实际的等级和形状论证不必与虚拟论证的等级和形状相吻合,但哑元参数中的元素数量不得超过实际参数的元素序列中的元素数.如果虚拟参数假定为-size,即虚拟参数恰好是元素中元素的数量顺序.

4 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屋!

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