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

8
  • 1
    Please try to create an entire (but minimal) example for us to look at, along the lines of minimal reproducible example. Without that we'll have to guess many aspects. Commented Jan 14, 2019 at 22:01
  • 1
    How are dim and nndsdeclared? Is the code you show in the main program or in a subroutine? There is not enough information here for anyone to help. Commented Jan 14, 2019 at 22:01
  • 1
    Does your problem go away if you find an option, particular to your compiler, to put the automatic array on heap rather than stack? There are several other questions relating to that concept, but it should be easy enough for you to search the compiler documentation rather than those questions. Commented Jan 14, 2019 at 22:45
  • 2
    @carlos, it means that when you pass the two arguments into the routine and try to create what is known as an automatic array, the array is placed on the process's stack. The amount of memory required is roughly 27 MB. If your stack does not have enough space, the process dies. Using the ALLOCATABLEattribute and doing your own memory allocation puts the array on the heap. A user process typically has much more heap than stack. Commented Jan 15, 2019 at 1:00
  • 1
    Please copy the error messages here as text not just in a screenshot. Avoid screenshots where not absolutely necessary. The error messages must be searchable so that people with the same problem can find your question. This is really very important. When uploading a picture it always tells you this in the instructions, please do read them. Commented Jan 15, 2019 at 7:03

1 Answer 1

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."

Sign up to request clarification or add additional context in comments.

2 Comments

According to the description I would expect setting "Heap Arrays" should work as 0 well. Just don't leave it blank. Or not?
@VladimirF n is the minimum size in kilobytes of the array to be allocated on the heap. If set to 0 all arrays will be allocated there, rather than on the stack. Although in my case, whatever n I use, the code will rune fine, as long as it's not empty (which is the default).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.