函数定义不明确的形状返回数组 [英] function returning array with no defined explicit shape
问题描述
我想知道如何从一个函数没有形状的任何知识返回数组,直到运行时(其中包括承担状阵列)。我将举例说明。这适用
模块富
包含
功能的getArray(:)
::实的getArray(3)
整数::我
做我= 1,3
的getArray(I)= 10.0 *我
ENDDO
最终功能前端模块XX程序
使用富
实::阵列(3)
整数::我 阵列=的getArray()
打印*,阵列
程序结束
这也适用,因为它使用的自动阵列
模块富
包含
功能的getArray(长度)
整数::长度
::实的getArray(长)
整数::我
做I = 1,长度
的getArray(I)= 10.0 *我
ENDDO
最终功能前端模块XX程序
使用富
实::阵列(5)
整数::我 阵=的getArray(5)
打印*,阵列
程序结束
这一个呢?它是有效的Fortran?我有内存泄漏在这种情况下
模块富
包含
功能的getArray()
真正的,可分配的getArray ::(:)
整数::长度
整数::我 长度= 5!来,例如,从磁盘 分配(的getArray(长)) 做I = 1,长度
的getArray(I)= 10.0 *我
ENDDO !不能调用DEALLOCATE()或发生崩溃
最终功能前端模块使用富
实::阵列(5,5)!从其它途径获得最大的尺寸,所以有足够的空间
整数::我 阵列=的getArray()
!这里泄漏内存?意外的行为?
程序结束
函数返回一个分配数组这种能力是通过TR提供15581.见的 http://www.nag.co.uk/nagware/np/doc/tr.asp 。该功能必须分配数组和已使用后的可分配数组函数的结果自动释放,即没有内存泄漏!
另请参见在 HTTP的讨论://www.tek -tips.com/viewthread.cfm?qid=1613318&page=5 并的 http://software.intel.com/en-us/blogs/2008/03/31/doctor-it-hurts-when-i -DO - 这/ 。
2003 Fortran语言的另一个新特点,实施后,将允许你改变真正::阵列(5,5),以宣布阵列作为可分配也,它会在被自动分配到正确的大小分配 - 无需pre-分配它。好简单!这是英特尔Fortran的版本月底上市,而不是积极的默认。看到上面的最后一个环节。
I am wondering how to return an array from a function without any knowledge of the shape until runtime (including assumed shape arrays). I'll explain with examples. This works
module foo
contains
function getArray(:)
real :: getArray(3)
integer :: i
do i=1,3
getArray(i) = 10.0*i
enddo
end function
end module
program xx
use foo
real :: array(3)
integer :: i
array = getArray()
print *, array
end program
This also works, because it makes use of automatic arrays
module foo
contains
function getArray(length)
integer :: length
real :: getArray(length)
integer :: i
do i=1,length
getArray(i) = 10.0*i
enddo
end function
end module
program xx
use foo
real :: array(5)
integer :: i
array = getArray(5)
print *, array
end program
What about this one? is it valid Fortran? do I have a memory leak in this case
module foo
contains
function getArray()
real, allocatable :: getArray(:)
integer :: length
integer :: i
length = 5 ! coming, for example, from disk
allocate(getArray(length))
do i=1,length
getArray(i) = 10.0*i
enddo
! cannot call deallocate() or a crash occurs
end function
end module
use foo
real :: array(5,5) ! get max size from other means, so to have enough space
integer :: i
array = getArray()
! leaking memory here ? unexpected behavior ?
end program
This ability of a function to return an allocatable array is provided by TR 15581. See http://www.nag.co.uk/nagware/np/doc/tr.asp. The function must allocate the array and "The result of an allocatable array function is automatically deallocated after it has been used", i.e., no memory leak!
Also see the discussions at http://www.tek-tips.com/viewthread.cfm?qid=1613318&page=5 and http://software.intel.com/en-us/blogs/2008/03/31/doctor-it-hurts-when-i-do-this/.
Another new feature of Fortran 2003, when implemented, will allow you to change "real :: array(5,5)" to declaring "array" as an allocatable also, and it will be automatically allocated to the correct size upon assignment -- no need to pre-allocate it. Very easy! This is available in the late versions of Intel Fortran, but is not active by default. See the last link above.
这篇关于函数定义不明确的形状返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!