【问题标题】:Common block and subroutine argument公共块和子程序参数
【发布时间】:2017-04-17 15:24:34
【问题描述】:

如果我有一个名为 var 的变量位于名为 myCB 的公共块中,我可以使用相同的名称在不使用公共块 myCB 的其他两个子例程之间传递参数吗?

代码如下。

Subroutine SR1(Var)
      !something here using Var
end Subroutine SR1

Subroutine SR2()
....
      Call SR1(B)
....
end Subroutine SR2

Subroutine SR3()
common \myCB\ Var
... 
  ! something using the other Var shared with SR4
......
end Subroutine SR3

Subroutine SR4()
common \myCB\ Var
....
... ! something using the other Var shared with SR3
....
end Subroutine SR4

VarSR1SR2 之间传递确实有问题,问题可能来自公共块中另一个名为Var 的问题吗?

【问题讨论】:

  • 我认为您需要给出一个更完整的示例(请参阅minimal reproducible example),并说明您认为存在问题的原因。就目前的问题而言,我们将不得不猜测。范围内有一些重要的东西,但是当前的代码片段中缺少很多东西,以至于无法分辨。
  • 整个代码是 2600 行,但我会考虑如何通过代码中的更多细节来编辑我的帖子更具体,谢谢

标签: variables fortran subroutine fortran-common-block


【解决方案1】:

如果你不想过多修改遗留代码库,我建议你将common块放在module中,并在需要访问时导入变量:

module myCB_mod
    common /myCB/ var, var2, var3
    save ! This is not necessary in Fortran 2008+
end module myCB_mod

subroutine SR2()
    use myCB_mod
    !.......
    call SR1(B)
    !.....
end subroutine SR2

subroutine SR3()
    use myCB_mod
    !.......
end subroutine SR3

subroutine SR4()
    use myCB_mod
    !.....
end subroutine SR4

或者更好的是,我建议您完全避免使用 common 块(这需要完全重写遗留代码库)并将所有子例程限制在 module

module myCB
    implicit none
    real var, var2, var3
    save ! This is not necessary in Fortran 2008+
end module myCB

module mySubs
    use myCB
    implicit none
contains
    subroutine SR2()
            !.......
            call SR1(B)
            !.....
    end subroutine SR2

    subroutine SR3()
            !.......
    end subroutine SR3

    subroutine SR4()
            !.....
    end subroutine SR4
end module

最后,common 块中的变量是否需要初始化?如果是这样,这将引入涉及data 语句甚至block data 构造的更多复杂性。

【讨论】:

    猜你喜欢
    • 2013-05-23
    • 2019-11-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 2014-04-05
    • 2011-03-01
    相关资源
    最近更新 更多