【问题标题】:Unable to print allocated status of a allocatable inside a derived type无法在派生类型中打印可分配的分配状态
【发布时间】:2019-11-06 15:26:51
【问题描述】:

我想知道为什么这段代码在上次打印时返回错误。

使用 gfortran 7.4.0 失败,但使用 ifort 18.0.3 运行良好。

program test
implicit none
type :: syntax
  integer, allocatable :: f(:)
end type
type(syntax), allocatable :: rhs(:)

allocate(rhs(2))
print*, allocated(rhs(2)%f)
print*, allocated(rhs(size(rhs))%f)
end program

gfortran 错误是:

 F

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7f4cf40442da in ???
#1  0x7f4cf4043503 in ???
#2  0x7f4cf3c76f1f in ???
#3  0x55aa522e5e50 in test
    at /home/pena/Escritorio/c.f90:10
#4  0x55aa522e5f0d in main
    at /home/pena/Escritorio/c.f90:11
Violación de segmento (`core' generado)

【问题讨论】:

  • @francescalus 我刚刚在帖子中包含了错误。
  • ifort 的结果是什么?在 2 个打印语句 print *, size(rhs) 之间放置时打印的内容。使用 gfortran 7.3.0 (Cygwin) 我得到 F 2 T
  • @albert ifort给出的解决方案是:F F

标签: fortran gfortran allocatable-array


【解决方案1】:

这是 gfortran 中的一个错误,版本 8 中不存在。

如果你不能升级你的编译器,那么有一个简单的替代方案:只需为size(rhs) 使用一个临时变量:

hack = SIZE(rhs)
print*, allocated(rhs(hack)%f)

使用 gfortran 7.4.0 提供输出:

 F
 F

【讨论】:

  • 谢谢,我会做的。
猜你喜欢
  • 2020-03-17
  • 2013-05-01
  • 2020-08-25
  • 2018-11-22
  • 1970-01-01
  • 2014-10-18
  • 2023-03-30
  • 2019-12-17
  • 1970-01-01
相关资源
最近更新 更多