fortran - 指向派生类型的指针的声明可以是未定义的吗?
问题描述
我有一个小代码声明一个指向派生类型数组的指针,该数组具有一个字段,该字段是另一个派生类型的可分配数组,具有一个实变量作为字段。使用 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 在屏幕上获得“失败”并且上面的打印不一致,使用英特尔编译器在屏幕上获得“成功”并且上面的打印一致。
解决方案
It's not clear to me what your question is, unless it's the title. If so, no, pointers to derived types are fine. Your program correctly allocates std as an extent-1 array and then allocates std(1)%np(2). It then assigns to the x subcomponents of both np elements. Looks fine to me.
There are several things that could potentially cause "failure" - you should run the gfortran code in the debugger to see what is going wrong.
推荐阅读
- vue.js - 模拟函数在 Vue 测试中没有返回值
- python - 如何处理棋盘格中的鼠标光标移动事件?
- c# - RabbitMq like 与 Masstransit 的话题交流
- powerapps - 共享 Power App 时无法将安全角色分配给 Dataverse 表
- python - 使用列表作为函数的参数
- java - Spring Batch JpaTransactionManager 会影响 Spring Data JPA?
- python - 我不会在我的游戏石头,纸和剪刀中输
- oauth2-playground - 多个并发访问代码请求/上传
- php - MySQL - 按星期几比较几个月的数据 - 例如每个月的第一个星期六的会话
- swift - 如何在 Xcode 13 中使用图像文字