【问题标题】:Fortran segmentation fault in pointer array指针数组中的 Fortran 分段错误
【发布时间】:2012-08-09 02:54:03
【问题描述】:

我在 fortran 中遇到了分段错误问题。 我通过调用一个子程序分配一个指针数组并将这个数组传递给另一个子程序。

我在 Linux 机器上用 PGI fortran 9.0.2 编译了这个程序。 这是我的测试程序。我该如何解决这个问题?

非常感谢您的帮助。

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

这是输出。

 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

【问题讨论】:

  • DaveP 在下面有一个正确答案。此外,如果您使用 -C(检查边界)编译您的程序,您将收到一条非常有用的消息,说明您的程序出了什么问题。

标签: segmentation-fault fortran


【解决方案1】:

你需要把主程序的最后一行改成:

call hoge(foox%x)

call hoge(foox)

如果您在模块中定义了例程 hoge 并将其导入,那么编译器将拾取此类型错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 2014-10-09
    • 2012-07-09
    • 2018-07-29
    • 1970-01-01
    相关资源
    最近更新 更多