我正在开发一个面向对象的Fortran代码,用于数值优化,抽象类型支持多态。因为它是一个很好的TDD实践,我试图在抽象类型class(generic_optimizer)
中编写所有优化测试,然后应该由每个实例化的类运行,例如type(newton_raphson)
。
所有优化测试都以call my_problem%solve(...)
为特色,在抽象类型中定义为deferred
,当然在每种派生类型中都有不同的实现。
问题是:如果在每个非抽象类中我将延迟函数定义为non_overridable
,我会得到分段错误,例如:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) where
#0 0x0000000000000000 in ?? ()
#1 0x0000000000913efe in __newton_raphson_MOD_nr_solve ()
#2 0x00000000008cfafa in MAIN__ ()
#3 0x00000000008cfb2b in main ()
#4 0x0000003a3c81ed5d in __libc_start_main () from /lib64/libc.so.6
#5 0x00000000004048f9 in _start ()
经过一些反复试验后,我注意到如果删除non_overridable
声明,我可以避免错误。在这种情况下,它不是一个问题,但我想强制执行,因为此代码不太可能有两个级别的多态。我是否违反了标准的任何要求?
以下是重现错误的示例代码。我一直用gfortran 5.3.0和6.1.0进行测试。
module generic_type_module
implicit none
private
type, abstract, public :: generic_type
real(8) :: some_data
contains
procedure (sqrt_interface), deferred :: square_root
procedure, non_overridable :: sqrt_test
end type generic_type
abstract interface
real(8) function sqrt_interface(this,x) result(sqrtx)
import generic_type
class(generic_type), intent(in) :: this
real(8), intent(in) :: x
end function sqrt_interface
end interface
contains
subroutine sqrt_test(this,x)
class(generic_type), intent(in) :: this
real(8), intent(in) :: x
print *, 'sqrt(',x,') = ',this%square_root(x)
end subroutine sqrt_test
end module generic_type_module
module actual_types_module
use generic_type_module
implicit none
private
type, public, extends(generic_type) :: crashing
real(8) :: other_data
contains
procedure, non_overridable :: square_root => crashing_square_root
end type crashing
type, public, extends(generic_type) :: working
real(8) :: other_data
contains
procedure :: square_root => working_square_root
end type working
contains
real(8) function crashing_square_root(this,x) result(sqrtx)
class(crashing), intent(in) :: this
real(8), intent(in) :: x
sqrtx = sqrt(x)
end function crashing_square_root
real(8) function working_square_root(this,x) result(sqrtx)
class(working), intent(in) :: this
real(8), intent(in) :: x
sqrtx = sqrt(x)
end function working_square_root
end module actual_types_module
program deferred_test
use actual_types_module
implicit none
type(crashing) :: crashes
type(working) :: works
call works%sqrt_test(2.0_8)
call crashes%sqrt_test(2.0_8)
end program
答案 0 :(得分:0)
为了缩小问题范围,我从OP的代码中删除了抽象属性和数据成员,以便
module types
implicit none
type :: Type1
contains
procedure :: test
procedure :: square => Type1_square
endtype
type, extends(Type1) :: Type2
contains
procedure, non_overridable :: square => Type2_square
endtype
contains
subroutine test( this, x )
class(Type1) :: this
real :: x
print *, "square(", x, ") = ",this % square( x )
end subroutine
function Type1_square( this, x ) result( y )
class(Type1) :: this
real :: x, y
y = -100 ! dummy
end function
function Type2_square( this, x ) result( y )
class(Type2) :: this
real :: x, y
y = x**2
end function
end module
program main
use types
implicit none
type(Type1) :: t1
type(Type2) :: t2
call t1 % test( 2.0 )
call t2 % test( 2.0 )
end program
使用此代码,gfortran-6给出了
square( 2.00000000 ) = -100.000000
square( 2.00000000 ) = -100.000000
而ifort- {14,16}和Oracle fortran 12.5给出
square( 2.000000 ) = -100.0000
square( 2.000000 ) = 4.000000
我也尝试用子程序替换函数(打印实际调用的例程):
subroutine test( this, x )
class(Type1) :: this
real :: x, y
call this % square( x, y )
print *, "square(", x, ") = ", y
end subroutine
subroutine Type1_square( this, x, y )
class(Type1) :: this
real :: x, y
print *, "Type1_square:"
y = -100 ! dummy
end subroutine
subroutine Type2_square( this, x, y )
class(Type2) :: this
real :: x, y
print *, "Type2_square:"
y = x**2
end subroutine
所有其他部分保持不变。然后,gfortran-6给出了
Type1_square:
square( 2.00000000 ) = -100.000000
Type1_square:
square( 2.00000000 ) = -100.000000
而ifort- {14,16}和Oracle fortran 12.5给出
Type1_square:
square( 2.000000 ) = -100.0000
Type2_square:
square( 2.000000 ) = 4.000000
如果我从上述代码中删除non_overridable
,gfortran会提供与其他编译器相同的结果。所以,这可能是gfortran + non_overridable
的一个特定问题(如果上面的代码符合标准)......
(OP出现分段错误的原因可能是gfortran访问了具有空指针的父类型(deferred
)中的generic_type
过程;如果是这种情况,则故事变得一致。)
修改强>
当我们将Type1声明为abstract
时,gfortran的异常行为也会发生。具体来说,如果我们将Type1的定义更改为
type, abstract :: Type1 ! now an abstract type (cannot be instantiated)
contains
procedure :: test
procedure :: square => Type1_square
endtype
和主程序
program main
use types
implicit none
type(Type2) :: t2
call t2 % test( 2.0 )
end program
我们得到了
ifort-16 : square( 2.000000 ) = 4.000000
oracle-12.5 : square( 2.0 ) = 4.0
gfortran-6 : square( 2.00000000 ) = -100.000000
如果我们进一步使Type1中的square()
为deferred
(即没有给出实现),那么使代码几乎等同于OP的情况,
type, abstract :: Type1 ! now an abstract type (cannot be instantiated)
contains
procedure :: test
procedure(Type1_square), deferred :: square ! has no implementation yet
endtype
abstract interface
function Type1_square( this, x ) result( y )
import
class(Type1) :: this
real :: x, y
end function
end interface
然后ifort-16和Oracle-12.5给出4.0 call t2 % test( 2.0 )
,而gfortran-6导致分段错误。的确,如果我们编译为
$ gfortran -fsanitize=address test.f90 # on Linux x86_64
我们得到了
ASAN:SIGSEGV (<-- or "ASAN:DEADLYSIGNAL" on OSX 10.9)
=================================================================
==22045==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000
(pc 0x000000000000 bp 0x7fff1d23ecd0 sp 0x7fff1d23eac8 T0)
==22045==Hint: pc points to the zero page.
总的来说,好像gfortran(可能带有空指针)错误地调用Type1中的绑定名square()
(没有实现)。更重要的是,如果我们从Type2的定义中删除non_overridable
,gfortran也会给出4.0(没有分段错误)。