linux下pgf90编译器第二次调用子程序出现分段错误
我无法在 Linux 环境下使用 pgf90 fortran 编译器调用同一子例程两次。第一次调用该子程序没问题,但第二次调用它时,就会出现分段错误。有人可以给我一些建议吗,我的代码有什么问题,作为简单的例子,用
gfortran 给出 PS 就可以了,即使我在 intel Visual fortran 上尝试过,它也可以
program main
use module_Append_1DI
implicit none
integer, allocatable:: Arr(:)
integer::Brr(2)
Brr=[3, 4]
call Append_1DI(Arr,Brr)
write(*,*)Arr
call Append_1DI(Arr,Brr)
write(*,*)Arr
end program main
module module_Append_1DI
contains
subroutine Append_1DI(A,B)
implicit none
!================================================
integer, allocatable, intent(inout)::A(:)
integer, intent(in)::B(:)
integer, allocatable::temp(:)
integer::sizeA,sizeB,sizeN
!================================================
sizeA=size(A); sizeB=size(B); sizeN=sizeA+sizeB
allocate(temp(sizeN)); temp(1:sizeA)=A
call move_alloc(from=temp,to=A)
A(sizeA+1:sizeN)=B
end subroutine Append_1DI
end module module_Append_1DI
I can not call the same subroutine two time using pgf90 fortran complier on Linux environment. To call the subroutine for the 1st time is OK but calling it for the 2nd time, it gives Segmentation fault. Can some one give some suggestion, what is wrong with my code, As simple example is given as
P.S. with gfortran it is OK, even i tried it on the intel visual fortran and it is OK
program main
use module_Append_1DI
implicit none
integer, allocatable:: Arr(:)
integer::Brr(2)
Brr=[3, 4]
call Append_1DI(Arr,Brr)
write(*,*)Arr
call Append_1DI(Arr,Brr)
write(*,*)Arr
end program main
module module_Append_1DI
contains
subroutine Append_1DI(A,B)
implicit none
!================================================
integer, allocatable, intent(inout)::A(:)
integer, intent(in)::B(:)
integer, allocatable::temp(:)
integer::sizeA,sizeB,sizeN
!================================================
sizeA=size(A); sizeB=size(B); sizeN=sizeA+sizeB
allocate(temp(sizeN)); temp(1:sizeA)=A
call move_alloc(from=temp,to=A)
A(sizeA+1:sizeN)=B
end subroutine Append_1DI
end module module_Append_1DI
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说实话,当你第一次调用它时,我很惊讶它竟然能起作用。这是因为 A 尚未分配,并且不允许您在未分配的可分配数组上使用内部大小。事实上,如果你打开所有检查标志,ifort 会告诉你这个
gfortran 不太清楚,但仍然告诉你有些问题
并且选择另一个随机编译器,来自 Sun/oracle,你再次得到相同的消息
所以问题是在分配 A 之前使用它。
您是否认为这是一个零大小的数组感到困惑?好吧,您需要将其从头脑中清除 - 未分配的可分配数组根本没有定义的大小,这与已分配的零大小数组非常不同。
伊恩
To be honest I'm amazed it works the first time you call it. That's because A is not then allocated, and you are not allowed to use the size intrinsic on an unallocated allocatable array. In fact if you turn on all the checking flags ifort tells you this
gfortran is less clear, but still tells you something is wrong
And to pick another random compiler, that from Sun/oracle, again you get the same message
So the problem is the use of A before it is allocated.
Is the confusion that you think this is a zero size array? Well you need to get that out of your head - an unallocated alloctable array has no defined size at all, which is very different from allocated bu zero size array.
Ian