指向派生类型的指针声明是否不确定?

时间:2019-07-03 18:04:21

标签: fortran gfortran

我有一个小代码,声明了一个指向派生类型数组的指针,该数组具有一个字段,该字段是具有实变量作为字段的另一个派生类型的可分配数组。 使用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可以在屏幕上获得“失败”并且上面的打印不一致,而使用Intel编译器则可以在屏幕上获得“成功”并且上面的打印一致。

2 个答案:

答案 0 :(得分:2)

我不清楚您的问题是什么,除非是标题。如果是这样,则否,指向派生类型的指针就可以了。您的程序正确地将std分配为一个区1数组,然后分配std(1)%np(2)。然后,将其分配给两个np元素的x个子组件。对我来说很好。

有几件事可能会导致“失败”-您应该在调试器中运行gfortran代码,以查看发生了什么问题。

答案 1 :(得分:0)

上面的代码中出现的问题已在下面的[link] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91077

中解决。