Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Fortran子程序中的Stackoverflow_Arrays_Fortran_Stack Overflow - Fatal编程技术网

Arrays Fortran子程序中的Stackoverflow

Arrays Fortran子程序中的Stackoverflow,arrays,fortran,stack-overflow,Arrays,Fortran,Stack Overflow,我有一个带有子程序的主程序,需要多次调用。主程序如下所示: program main open output file do i = 1, 20000 read parameters par_1, par_2, ..., par_8 call subroutine (par_1, .... , par_8) enddo end program subroutine calcr real, dimension(5000) :: array_1, array_2, .

我有一个带有子程序的主程序,需要多次调用。主程序如下所示:

program main
 open output file
   do i = 1, 20000
   read parameters par_1, par_2, ..., par_8
   call subroutine (par_1, .... , par_8)
   enddo
end program
subroutine calcr
  real, dimension(5000) :: array_1, array_2, .... array_20
  read temperature into array_1
  read pH into array_2
  ...   
  store intermediate results into array_10
  sotre intermediate results into array_20
  ...
  make final calculations 
  write the results to the output file
  close files from which the data was read (temperature, pH...)
end subroutine
子例程完成了所有工作,我不想保存数组的值。它们主要用于存储中间结果

子例程如下所示:

program main
 open output file
   do i = 1, 20000
   read parameters par_1, par_2, ..., par_8
   call subroutine (par_1, .... , par_8)
   enddo
end program
subroutine calcr
  real, dimension(5000) :: array_1, array_2, .... array_20
  read temperature into array_1
  read pH into array_2
  ...   
  store intermediate results into array_10
  sotre intermediate results into array_20
  ...
  make final calculations 
  write the results to the output file
  close files from which the data was read (temperature, pH...)
end subroutine
我发现20个阵列中有两个存在问题。如果我将这两个数组的维数增加一倍,我可以毫无问题地运行程序两次。 程序停止时显示错误消息“超出程序异常数组绑定”

如果我取维数*10,那么我可以运行程序10次,得到相同的错误

但是如果我取维*100,我只能运行程序大约30次,并得到错误“程序异常-堆栈溢出”

我不知道问题可能出在哪里,因为我以相同的方式处理所有数组,只有两个数组存在此问题。
谢谢大家!

如果没有实际代码,问题很难诊断。子程序是否知道主程序中的循环和计数器或调用子程序的次数?我猜对于这两个特殊数组,您是通过使用主循环中的计数器I计算的索引来访问元素的

你在用什么编译器?对于gfortran,有一个标志来测试数组是否超出范围,即。对于其他编译器,可能有类似的标志。您可以使用这些标志进行编译,希望您能够获得有关数组越界问题发生在何处的更多信息,例如,您的编译器调用

“超出程序异常数组绑定”

但是没有给出导致问题的代码行的信息

这是一个我认为你正在做的程序:

module global
  integer :: nsubcalls=0 ! counter for the number of times stuff() is called
end module

subroutine stuff
  use global
  integer :: isub
  integer :: nelements = 5000
  real,dimension(5000) :: array_1

  do isub=1,nelements
     array_1(nsubcalls*nelements+isub) = isub
  end do

  nsubcalls = nsubcalls +1 
end subroutine

program main 

  use global 
  integer :: i

  nsubcalls=0
  do i=0,20000
     print *,"i = ",i
     call stuff()
  end do

end program
如果我编译这个,像这样:

 gfortran -fbounds-check test2.f90
我得到以下输出:

i =            0
i =            1
At line 15 of file test2.f90
Fortran runtime error: Array reference out of bounds for array 'array_1', upper bound of dimension 1 exceeded (5001 > 5000)

不清楚为什么会出现堆栈溢出,但我猜您最终会遇到这样的情况:数组占用了太多内存而无法启动。如果你能提供更多的信息,我也许能提供更多的帮助。您的代码实际上是什么样子的?

如果没有实际的代码,问题很难诊断。子程序是否知道主程序中的循环和计数器或调用子程序的次数?我猜对于这两个特殊数组,您是通过使用主循环中的计数器I计算的索引来访问元素的

你在用什么编译器?对于gfortran,有一个标志来测试数组是否超出范围,即。对于其他编译器,可能有类似的标志。您可以使用这些标志进行编译,希望您能够获得有关数组越界问题发生在何处的更多信息,例如,您的编译器调用

“超出程序异常数组绑定”

但是没有给出导致问题的代码行的信息

这是一个我认为你正在做的程序:

module global
  integer :: nsubcalls=0 ! counter for the number of times stuff() is called
end module

subroutine stuff
  use global
  integer :: isub
  integer :: nelements = 5000
  real,dimension(5000) :: array_1

  do isub=1,nelements
     array_1(nsubcalls*nelements+isub) = isub
  end do

  nsubcalls = nsubcalls +1 
end subroutine

program main 

  use global 
  integer :: i

  nsubcalls=0
  do i=0,20000
     print *,"i = ",i
     call stuff()
  end do

end program
如果我编译这个,像这样:

 gfortran -fbounds-check test2.f90
我得到以下输出:

i =            0
i =            1
At line 15 of file test2.f90
Fortran runtime error: Array reference out of bounds for array 'array_1', upper bound of dimension 1 exceeded (5001 > 5000)

不清楚为什么会出现堆栈溢出,但我猜您最终会遇到这样的情况:数组占用了太多内存而无法启动。如果你能提供更多的信息,我也许能提供更多的帮助。您的代码实际上是什么样子的?

真正的问题可能是“超出数组边界”错误——这表明存在错误,即导致程序尝试访问数组之外的数组元素的编码错误。非法内存访问可能会导致其他错误。将阵列大小增加到超出您认为需要的大小(如果您正在这样做的话)是解决实际问题的一个很差的方法。我建议在继续之前解决数组边界错误。为什么索引是真正的问题可能是“超出数组边界”错误——这表明存在错误,即导致程序尝试访问数组之外的数组元素的编码错误。非法内存访问可能会导致其他错误。将阵列大小增加到超出您认为需要的大小(如果您正在这样做的话)是解决实际问题的一个很差的方法。我建议在继续之前解决数组边界错误。为什么索引不可能用你提供的信息给你答案。用你提供的信息给你答案是不可能的。