Fortran数组分配溢出

时间:2019-01-14 21:56:54

标签: arrays fortran stack overflow allocation

我是Fortran的新手,并且在模块内的子例程中,我试图声明以下变量:

real(kind = 8), dimension(dim*nnds, dim*nnds) :: Kgel

我得到以下信息:

Unhandled exception at 0x009F4029 in Solver.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00602000).

dim和nnds是来自另一个模块的变量,我知道它们被正确地传递为:

integer(kind = 8) :: nnds, dim
dim = 2
nnds = 937

如果我像这样声明变量:

real(kind = 8), dimension(2*937, 2*937) :: Kgel

甚至像:

real(kind = 8), dimension(:, :), allocatable :: Kgel

allocate(Kgel(dim*nnds, dim*nnds))

它有效,所以为什么我不能这样声明'Kgel':

real(kind = 8), dimension(dim*nnds, dim*nnds) :: Kgel

非常感谢您的宝贵时间...

更新

我的代码是这样的:

program MainTest

    use FirstModule
    use SecondModule

    call FirstSubRoutine
    call SecondSubRoutine

end program

module FirstModule

    integer(kind = 8) :: nnds, dim

    contains

    subroutine FirstSubRoutine()

        !does stuff and eventually
        dim = 2
        nnds = 937

    end subroutine

end module

module SecondModule

    use FirstModule

    contains

    subroutine SecondSubRoutine()

        real(kind = 8), dimension(nnds*dim, nnds*dim) :: Kgel
        !print *, dim -> dim = 2
        !print *, nnds -> nnds = 937
        !pause
            !but this works:
            !real(kind = 8), dimension(:, :), allocatable :: Kgel
            !allocate(Kgel(dim*nnds, dim*nnds))

    end subroutine

end module

这个小的测试代码将重现我的问题。

更新

通过更改“堆栈保留大小”,现在看来可以正常工作:

enter image description here

1 个答案:

答案 0 :(得分:1)

要解决此问题,需要像下面这样声明变量:

real(kind = 8), dimension(:, :), allocatable :: Kgel
!...
allocate(Kgel(dim*nnds, dim*nnds))

或者,因为我在Visual Studio上使用Intel Fortran编译器,所以在“项目属性”>“配置属性”>“ Fortran”>“优化”中将堆数组从0设置为n:

“堆数组:在堆而不是栈上分配最小大小为n(以千字节为单位)的临时数组。使用0始终将它们分配在堆上。留空不激活。”

相关问题