【问题标题】:Segmentation fault with optional arguments in Fortran functionsFortran 函数中带有可选参数的分段错误
【发布时间】:2013-08-29 10:43:35
【问题描述】:

我可以将 Fortran 可选参数与 intent(in)intent(inout) 的子例程一起使用,但函数可选参数仅适用于 intent(in),对吗?使用intent(inout),我在以下代码中遇到分段错误:

real function foo(x, tol) 
    real, intent(in) :: x
    real, optional, intent(inout) :: tol
    if( .not. present(tol) ) tol = 1e-6
    !...
end function foo

【问题讨论】:

标签: function fortran optional-parameters


【解决方案1】:

我发现了问题,即使在第四行(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++ 中我们做类似的事情时

double foo(double x, double tol=1e-6)

不幸的是,这似乎在 Fortran 中是不可能的。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 2013-12-07
  • 1970-01-01
  • 2018-06-17
相关资源
最近更新 更多