指针数组中的Fortran分段错误 [英] Fortran segmentation fault in pointer array

查看:210
本文介绍了指针数组中的Fortran分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Fortran中我有一个分段错误问题。
我通过调用一个子例程来分配一个指针数组,并将这个数组传递给另一个子例程。

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



非常感谢您的帮助。

 模块hogehoge 
隐式无
类型foo
真实,指针:: x(:)=> null()
结束类型foo
包含
子程序hogehoge_func(foox)
类型(foo),意图(inout):: foox
整数
分配(foox%x(2048))
do i = 1,2048
foox%x(i)=
end do
end子程序hogehoge_func
end模块hogehoge

!主程序---- --------------------------------------
程序测试
使用hogehoge
隐式无
类型(foo):: foox
调用hogehoge_func(foox)

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

结束程序测试
子程序hoge(foox)
使用hogehoge
隐含无
type(foo),intent(in):: foox
$ b $ print *,'in subroutine'
print *,foox%x(1)

结束子程序hoge

以下是输出。

<$ p主程序中的$ p>
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
子程序
分段错误


解决方案

您需要更改主程序的最后一行:

  call hoge(foox%x)

  call hoge(foox)

如果您定义了例程 hoge 在一个模块中并导入它,然后编译器会选择这种类型的错误。


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

解决方案

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.

这篇关于指针数组中的Fortran分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆