Fortran函数中带有可选参数的分段错误 [英] Segmentation fault with optional arguments in Fortran functions
问题描述
我可以将Fortran可选参数与带有intent(in)
和intent(inout)
的子例程一起使用,但是通过函数,可选参数只能与intent(in)
一起使用,对吗?使用intent(inout)
,我在以下代码中遇到分段错误:
I can use Fortran optional argumenrs with subroutines with intent(in)
and intent(inout)
, but with functions optional arguments work only with intent(in)
, right? With intent(inout)
I get segmentation faults in the following code:
real function foo(x, tol)
real, intent(in) :: x
real, optional, intent(inout) :: tol
if( .not. present(tol) ) tol = 1e-6
!...
end function foo
推荐答案
我发现了问题,即使在第四行(在tol = 1e-6
中)不存在,我也使用了变量:
I found the problem, I used the variable even when not present on the fourth line (in tol = 1e-6
):
real function foo(x, tol)
real, intent(in) :: x
real, optional, intent(inout) :: tol
if( .not. present(tol) ) tol = 1e-6
!...
end function foo
但是即使在不存在的情况下,我也想使用它并设置默认值,例如在C ++中,我们要做类似的事情
But I would like to use it even when not present and set a default value, like when in C++ we do something like that
double foo(double x, double tol=1e-6)
不幸的是,似乎在Fortran中是不可能的.
Unfortunately, it seems it is not possible in Fortran.
这篇关于Fortran函数中带有可选参数的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!