Nor*_*ico 4 fortran allocation error-checking subroutine
我想确定这两个选项中哪一个最好:
subroutine sqtrace( Msize, Matrix, Value )
integer, intent(in) :: Msize
real*8, intent(in) :: Matrix(Msize, Msize)
real*8, intent(out) :: Value
[instructions...]
end subroutine sqtrace
Run Code Online (Sandbox Code Playgroud)
VS
subroutine sqtrace( Matrix, Value )
real*8, intent(in) :: Matrix(:,:)
real*8, intent(out) :: Value
if ( size(Matrix,1) /= size(Matrix,2) ) then
[error case instructions]
end if
[instructions...]
end subroutine sqtrace
Run Code Online (Sandbox Code Playgroud)
我知道当你使用警告进行编译时,如果调用sqtrace符合指示的大小,第一种情况应该在编译时自动检查.但是,我不知道编译器是否可以在给定参数可分配时执行这些检查(例如,如果此类分配依赖于在运行时确定的其他事项,则更多如此).第二个需要一个显式接口,并有更多的代码(检查),但似乎会捕获更多的错误.
使用每种情况的优点/缺点是什么?在哪种情况下,哪一种情况应该与另一种情况一起使用?
首先,一些术语.考虑声明为的伪参数
real :: a(n) ! An explicit shape array
real :: b(:) ! An assumed shape array
real, allocatable :: c(:) ! A deferred shape array
real :: d(*) ! An assumed size array
Run Code Online (Sandbox Code Playgroud)
(延迟形状数组也可以是指针而不是可分配的).
在特定情况下,我不会回答哪个更好,但是会简单地详细说明一些重要的特征,如果有的话,给程序员留下选择.粗略地,许多人将显式形状阵列视为"Fortran 77",并假设形状阵列为"Fortran 90+".
形状:
Contiguousness:
对实际论点的限制:
调用范围中的接口:
考虑real a(6).这可能是假人的实际论据
real b(3)
real c(2,3)
real d(:) ! With an explicit interface available
Run Code Online (Sandbox Code Playgroud)
a(1::2)可能与之相关联,b但b同时涉及复制/复制.与之关联时不需要涉及复制/复制d,但可能是.
还有很多其他方面,但希望这是一个初步的高级别介绍.
| 归档时间: |
|
| 查看次数: |
609 次 |
| 最近记录: |