Fortran array allocation overflow

2

I'm new to Fortran and inside a subroutine which is inside a module I'm trying to declare the following variable:

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

And I get the following:

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

dim and nnds are variables coming from another module, and I know they are being passed correctly as:

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

If I declare the variable like:

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

Or even like:

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

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

It works, so why can't I declare 'Kgel' like:

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

Thanks very much for your time...

UPDATE

My code is something like this:

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

This small test code will replicate my problem.

UPDATE

By changing the "Stack Reserve Size" it now seems to work fine:

enter image description here

arrays
fortran
stack
overflow
allocation
asked on Stack Overflow Jan 14, 2019 by Carlos • edited Jan 15, 2019 by Carlos

1 Answer

1

To solve the issue, the variable needs to be declared like this:

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

Or, since I'm using the Intel Fortran Compiler on Visual Studio, on Project Properties > Configuration Properties > Fortran > Optimization set Heap Arrays from 0 to n:

"Heap Arrays: Allocate temporary arrays of minimum size n (in kilobytes) on the heap rather than on the stack. Use 0 to always allocate them on the heap. Leave blank not to activate."

answered on Stack Overflow Jan 15, 2019 by Carlos

User contributions licensed under CC BY-SA 3.0