Neo123 发表于 2022-6-1 16:59:14

函数传入和返回值均为包含动态数组的type

因为程序很大,简化之后的程序如下所示。原程序是采用ifortran采用参数化结构体编写,因为想对程序实现GPU并行,所以采用gfortran12.x编写。哪位大佬帮忙解答一下哪错了{:4_117:}
module ModParticle
implicit none
integer::x=4
type ParticleType
    real(4) , allocatable :: MassGas(:)
contains
    Procedure :: CopyParticle => CopyParticle

end type
contains ! Routines of this module

Subroutine CopyParticle( this , ID )
    integer(4), intent(in) ::ID
    class(ParticleType) :: this
    this%MassGas = 0.1
End Subroutine CopyParticle

subroutine CopyType(TempParticlesx,TempParticles)
    type(ParticleType)::TempParticles
    type(ParticleType)::TempParticlesx
    allocate(TempParticles%MassGas(x))
      TempParticlesx%MassGas = TempParticles%MassGas
    deallocate(TempParticles%MassGas)
end subroutine

end module
PROGRAM Main
use ModParticle
implicit none

integer::i
type(ParticleType)::TempParticles
type(ParticleType)::TempParticlesx
allocate(TempParticles%MassGas(x))
do i = 1, size(TempParticles%MassGas)
    call TempParticles%CopyParticle(i)
end do
write(*,*) TempParticles%MassGas

call CopyType(TempParticlesx,TempParticles)

write(*,*) TempParticlesx%MassGas
deallocate(TempParticles%MassGas)

END PROGRAM Main

风平老涡 发表于 2022-6-1 19:36:28

本帖最后由 风平老涡 于 2022-6-1 20:02 编辑

allocate(tempparticles%massgas(x)), 在 MAIN 和 SUBROUTINE COPYTYPE 中 重复 allocation。去掉 MAIN中的 allocate 语句。

Neo123 发表于 2022-6-1 20:03:15

风平老涡 发表于 2022-6-1 19:36
allocate(tempparticles%massgas(x)), 在 MAIN 和 SUBROUTINE COPYTYPE 中 重复 allocation。

这不是问题所在,因为x在module ModParticle中已经声明了,main中引用了use ModParticle

kyra 发表于 2022-6-2 10:31:47

Neo123 发表于 2022-6-1 20:03
这不是问题所在,因为x在module ModParticle中已经声明了,main中引用了use ModParticle ...

这就是问题所在。
重复分配了。一个可分配数组只能分配一次,要第二次分配必须先 Deallocate 释放。

与它在哪里声明无关。

Neo123 发表于 2022-6-2 13:38:32

kyra 发表于 2022-6-2 10:31
这就是问题所在。
重复分配了。一个可分配数组只能分配一次,要第二次分配必须先 Deallocate 释放。



你看到的“风平老涡”的帖子是后来修改的,之前的内容是说我没定义x。
将ModParticle中的allocate删除后,可以在ifortran和gpfortran中正常编译,但是我在gfortran中还是报错undefined reference to ‘CopyType_’

打这些字的时候感觉是gfortran编译器的问题,刚才连之前用gfortran测试过的最简单的程序也都莫名奇妙报错,但吃个饭以后再用gfortran编译程序都可以正常编译了,不太明白是电脑问题还是软件问题

zjk0112 发表于 2022-6-2 18:10:02

Neo123 发表于 2022-6-2 13:38
你看到的“风平老涡”的帖子是后来修改的,之前的内容是说我没定义x。
将ModParticle中的allocate删除后 ...

你要不把你测试报错的最简单程序发出来,我可以在我电脑上帮你测试一下是不是gfortran,或者gfortran版本的问题。

Neo123 发表于 2022-6-2 22:13:26

zjk0112 发表于 2022-6-2 18:10
你要不把你测试报错的最简单程序发出来,我可以在我电脑上帮你测试一下是不是gfortran,或者gfortran版本 ...

我自己测试发现同一个程序codeblocks所带的旧版本的gfortran版本经常提示报错,有时会正常编译,但最新的gfortran 12.1是没有问题的

zjk0112 发表于 2022-6-3 23:03:03

Neo123 发表于 2022-6-2 22:13
我自己测试发现同一个程序codeblocks所带的旧版本的gfortran版本经常提示报错,有时会正常编译,但最新的 ...

有时报错,有时正常?依我的经验,是不是初始化的问题。

Neo123 发表于 2022-6-4 16:28:58

zjk0112 发表于 2022-6-3 23:03
有时报错,有时正常?依我的经验,是不是初始化的问题。

好的,谢谢你的建议,我根据你的建议再看看。确实是有时报错,有时正常。

accding 发表于 2022-6-17 12:22:33

module里面,为何用两次contains?
页: [1] 2
查看完整版本: 函数传入和返回值均为包含动态数组的type