Fortran segmentation fault in pointer array

爱⌒轻易说出口 提交于 2019-12-12 00:26:56

问题


I have a segmentation fault problem in fortran. I allocate a pointer array by calling a subroutine and pass this array to another subroutine.

I compile this program by PGI fortran 9.0.2 on Linux machine. Here is my test program. How should I fix this problem?

Thank you very much for your help.

module hogehoge
  implicit none
  type foo
     real, pointer :: x(:)=>null()
  end type foo
contains
  subroutine hogehoge_func(foox)
    type(foo), intent(inout) :: foox
    integer i
    allocate(foox%x(2048))
    do i = 1, 2048
       foox%x(i)=i
    end do
  end subroutine hogehoge_func
end module hogehoge

!main program------------------------------------------
program test
  use hogehoge
  implicit none
  type(foo) :: foox
  call hogehoge_func(foox)

  print*, 'in main program'
  print*, foox%x(1:20)
  call hoge(foox%x)

end program test
subroutine hoge(foox)
  use hogehoge
  implicit none
  type(foo), intent(in) :: foox

  print*, 'in subroutine'
  print*, foox%x(1)

end subroutine hoge

Here is the output.

 in main program
    1.000000        2.000000        3.000000        4.000000
    5.000000        6.000000        7.000000        8.000000
    9.000000        10.00000        11.00000        12.00000
    13.00000        14.00000        15.00000        16.00000
    17.00000        18.00000        19.00000        20.00000
 in subroutine
Segmentation fault

回答1:


You need to change the last line of the main program from:

call hoge(foox%x)

to

call hoge(foox)

If you had defined the routine hoge in a module and imported that, then the compiler would pick up this type error.



来源:https://stackoverflow.com/questions/11876134/fortran-segmentation-fault-in-pointer-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!