gpt4 book ai didi

fortran - 最终程序的问题(gfortran 的段错误)

转载 作者:行者123 更新时间:2023-12-01 03:03:02 25 4
gpt4 key购买 nike

考虑以下小程序,它重现了我使用 gfortran gcc 9.2.0 for mac os 得到的段错误):

module stringmod

type :: str_t
character(len=:), allocatable :: s
contains
final :: finalize
end type str_t

type :: static_t
type(str_t) :: expr(3)
end type static_t

type :: dynamic_t
type(str_t), allocatable :: expr(:)
end type dynamic_t

contains

subroutine finalize ( x )
type(str_t), intent(in out) :: x

if (allocated(x%s)) deallocate(x%s)

end subroutine finalize

end module stringmod

use stringmod

implicit none

type(static_t ), allocatable :: stat(:)
type(dynamic_t), allocatable :: dyna(:)
integer :: i, j, n = 10

allocate(stat(n),dyna(n))

do i = 1, n
allocate(dyna(i)%expr(3))
end do

! No problem when deallocating "dyna":
deallocate(dyna)

! while deallocating "stat"causes a segfault with gfortran (gcc 9.2.0 for mac os)
! (unless the final method is removed from the DT):

deallocate(stat)

end

段错误仅在释放具有 str_t 类型显式数组成员的 static_t 类型数组时发生。奇怪的是,它似乎与 str_t DT 的 final方法有关(当我删除此过程时,问题就消失了),但我没有看到原因。

最佳答案

这确实看起来像最终子程序的问题,毕竟删除最终子程序可以消除段错误。然而,这是 gfortran 中更广泛的一类问题的一个例子。

对于 gfortran 中的一个问题,它是。

在您的情况下,您有一个可用的解决方法:删除在任何时候都未实际调用的最终子例程。在更一般的情况下,这可能不是一种选择。无论哪种方式,要做的就是将这个错误报告给 GCC 维护者(特别是如果您可以使用更高版本的编译器进行测试)。

为什么我说这部分是一个更广泛的问题?让我们来看看简化你的代码:

  type :: str_t
character, allocatable :: s
end type str_t

type :: dynamic_t
type(str_t), allocatable :: expr
end type dynamic_t

type(dynamic_t), allocatable :: dyna

allocate(dyna)
allocate(dyna%expr)

end

或者
  implicit none

type :: str_t
character, allocatable :: s
end type str_t

type :: static_t
type(str_t) :: expr
end type static_t

type(static_t), allocatable :: stat
allocate(stat)

end

两者都没有最终确定,但是在运行时都存在(对我而言)段错误。

如果你想看到问题的程序和这个答案之间的联系:它是组件的延迟长度性质 s这是关键。作为显式长度(可分配)标量组件,无论是否完成,上述示例都会崩溃;作为延迟长度组件完成控制崩溃。

关于fortran - 最终程序的问题(gfortran 的段错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59893286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com