【发布时间】:2015-09-08 08:02:02
【问题描述】:
我有一个代码,当我使用 RHS 上的结构构造函数对 LHS 上的未分配可分配对象进行分配时,该代码会与我手头的所有编译器发生段错误。结构(派生类型)本身具有重载赋值。我认为应该在调用分配例程之前自动重新分配 LHS,但似乎并非如此。
在代码下方,演示问题。取消注释分配语句使一切正常,但我不明白为什么在这种情况下需要显式分配。有趣的是,如果我删除了重载的赋值,一切都会正常进行。
有什么提示吗?
module dummy
implicit none
type :: DummyType
integer :: ii
contains
procedure :: assignDummyType
generic :: assignment(=) => assignDummyType
end type DummyType
interface DummyType
module procedure DummyType_init
end interface DummyType
contains
function DummyType_init(initValue) result(this)
integer, intent(in) :: initValue
type(DummyType) :: this
this%ii = initValue
end function DummyType_init
subroutine assignDummyType(this, other)
class(DummyType), intent(out) :: this
type(DummyType), intent(in) :: other
this%ii = other%ii + 1
end subroutine assignDummyType
end module dummy
program test_dummy
use dummy
implicit none
type(DummyType), allocatable :: aa
!allocate(aa) ! Should be covered via automatic reallocation...
aa = DummyType(42)
end program test_dummy
【问题讨论】:
-
没有看代码,但是你有什么版本的编译器?有些包含自动重新分配有点晚了。
-
来自 GNU、Intel 和 NAG 的最新版本。但显然,这似乎是标准的问题,而不是编译器的问题,如下 IanH 所述。
标签: fortran fortran2003 fortran2008