指向派生类型和默认初始化的 Fortran 指针
Fortran pointer to derived types and default initialisation
Fortran 2003 派生类型有一个很好的默认初始化功能。
type TTest
integer :: a
integer :: b = 1
integer, pointer :: p1, p2 => null()
end type TTest
那么任何声明的类型 (TTest) 变量都将默认初始化组件 b 和 p2。
考虑以下代码:
type (TTest), dimension(:), pointer :: varptr
type (TTest), dimension(:), allocatable :: varalloc
integer, parameter :: ndim = 1000
allocate( varptr(ndim))
allocate(varalloc(ndim))
能否保证varptr和varalloc的所有元素在分配后都会初始化b和p2成员?
是的,标准要求如此。只要你有一个派生类型的变量,它就会初始化这些组件。
F2008 4.5.4.6.3: If null-init appears for a pointer component, that
component in any object of the type has an initial association
status of disassociated (1.3) or becomes disassociated as specified in
16.5.2.4.
F2008 4.5.4.6.6 : If constant-expr appears for a nonpointer component, that component in
any object of the type is initially defined (16.6.3) or becomes defined as specified in 16.6.5 with the value determined from
constant-expr ...
Fortran 2003 派生类型有一个很好的默认初始化功能。
type TTest
integer :: a
integer :: b = 1
integer, pointer :: p1, p2 => null()
end type TTest
那么任何声明的类型 (TTest) 变量都将默认初始化组件 b 和 p2。
考虑以下代码:
type (TTest), dimension(:), pointer :: varptr
type (TTest), dimension(:), allocatable :: varalloc
integer, parameter :: ndim = 1000
allocate( varptr(ndim))
allocate(varalloc(ndim))
能否保证varptr和varalloc的所有元素在分配后都会初始化b和p2成员?
是的,标准要求如此。只要你有一个派生类型的变量,它就会初始化这些组件。
F2008 4.5.4.6.3: If null-init appears for a pointer component, that component in any object of the type has an initial association status of disassociated (1.3) or becomes disassociated as specified in 16.5.2.4.
F2008 4.5.4.6.6 : If constant-expr appears for a nonpointer component, that component in any object of the type is initially defined (16.6.3) or becomes defined as specified in 16.6.5 with the value determined from constant-expr ...