可以未定义指向派生类型的指针声明吗?
Could declaration of pointers to derived types be undefined?
我有一个小代码声明了一个指向派生类型数组的指针,它有一个字段是另一个派生类型的可分配数组,有一个实变量作为字段。
使用 gnu fortran (8.2) 我为数组中的每个位置或作为向量获得了不同的结果。
使用intel fortran (2019.4)编译成功
program test
implicit none
integer, parameter :: length = 2
real(8), dimension(length) :: a, b
integer :: i
type point
real(8) :: x
end type point
type stored
type(point), dimension(:), allocatable :: np
end type stored
type(stored), dimension(:), pointer :: std=>null()
allocate(std(1))
allocate(std(1)%np(length))
std(1)%np(1)%x = 0.3d0
std(1)%np(2)%x = 0.3555d0
do i = 1, length
write(*, "('std(1)%np(',i1,')%x = ',1e22.14)") i, std(1)%np(i)%x
end do
do i = 1, length
write(*, "('std(1)%np(1:',i1,') = ',2e22.14)") i, std(1)%np(1:i)%x
end do
a = std(1)%np(1:2)%x
b = [std(1)%np(1)%x, std(1)%np(2)%x]
if (norm2(a - b) .gt. 1d-3) then
write(*,*) 'failure'
else
write(*, *) 'success'
end if
end program test
代码成功终止,但使用 gfortran 获得 'failure' 在屏幕上和上面不一致的打印,使用 Intel 编译器在屏幕上获得 'success' 和上面一致的打印。
我不清楚你的问题是什么,除非是标题。如果是这样,不,指向派生类型的指针很好。您的程序正确地将 std 分配为 extent-1 数组,然后分配 std(1)%np(2)。然后它分配给两个 np 元素的 x 子组件。我觉得不错。
有几件事可能会导致 "failure" - 您应该 运行 调试器中的 gfortran 代码以查看问题所在。
上面代码中出现的问题已在以下 [link] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91077
中解决
我有一个小代码声明了一个指向派生类型数组的指针,它有一个字段是另一个派生类型的可分配数组,有一个实变量作为字段。 使用 gnu fortran (8.2) 我为数组中的每个位置或作为向量获得了不同的结果。
使用intel fortran (2019.4)编译成功
program test
implicit none
integer, parameter :: length = 2
real(8), dimension(length) :: a, b
integer :: i
type point
real(8) :: x
end type point
type stored
type(point), dimension(:), allocatable :: np
end type stored
type(stored), dimension(:), pointer :: std=>null()
allocate(std(1))
allocate(std(1)%np(length))
std(1)%np(1)%x = 0.3d0
std(1)%np(2)%x = 0.3555d0
do i = 1, length
write(*, "('std(1)%np(',i1,')%x = ',1e22.14)") i, std(1)%np(i)%x
end do
do i = 1, length
write(*, "('std(1)%np(1:',i1,') = ',2e22.14)") i, std(1)%np(1:i)%x
end do
a = std(1)%np(1:2)%x
b = [std(1)%np(1)%x, std(1)%np(2)%x]
if (norm2(a - b) .gt. 1d-3) then
write(*,*) 'failure'
else
write(*, *) 'success'
end if
end program test
代码成功终止,但使用 gfortran 获得 'failure' 在屏幕上和上面不一致的打印,使用 Intel 编译器在屏幕上获得 'success' 和上面一致的打印。
我不清楚你的问题是什么,除非是标题。如果是这样,不,指向派生类型的指针很好。您的程序正确地将 std 分配为 extent-1 数组,然后分配 std(1)%np(2)。然后它分配给两个 np 元素的 x 子组件。我觉得不错。
有几件事可能会导致 "failure" - 您应该 运行 调试器中的 gfortran 代码以查看问题所在。
上面代码中出现的问题已在以下 [link] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91077
中解决