无法在派生类型内部打印可分配的分配状态

问题描述 投票:0回答:1

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

使用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)
fortran gfortran allocatable-array
1个回答
1
投票

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

如果无法升级编译器,那么有一个简单的选择:只需对size(rhs)使用一个临时变量:

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

使用gfortran 7.4.0提供输出:

 F
 F
© www.soinside.com 2019 - 2024. All rights reserved.