Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Memory Allocation

rafadix08
Beginner
1,016 Views

Consider the following array declaration inside a subroutine:

Subroutine my_sub

Use Global_Var

real(8) array(N)

....

end subroutine my_subs

N is a parameter defined inside module Global_var

Is array an automatic array and hence allocated to the stack?
If yes, the only way for it to go to the heap is by making it allocatable? Or is there another way?

Thank you,
Rafael

0 Kudos
1 Solution
Steven_L_Intel1
Employee
1,016 Views
What are your compiler options? The default is that local arrays are statically allocated, but this could change if you added /Qopenmp, /parallel or /recursive.

View solution in original post

0 Kudos
4 Replies
Steven_L_Intel1
Employee
1,016 Views

If N is a PARAMETER constant, then this is an ordinary local array that is statically allocated by default. If N is a variable, then it is an automatic array on the stack - it can be placed on the heap by using the /heap-arrays option, though I would recommend making it ALLOCATABLE instead.
0 Kudos
rafadix08
Beginner
1,016 Views

If N is a PARAMETER constant, then this is an ordinary local array that is statically allocated by default. If N is a variable, then it is an automatic array on the stack - it can be placed on the heap by using the /heap-arrays option, though I would recommend making it ALLOCATABLE instead.

N is declared as a parameter inside a module (Global_var):
integer, parameter :: N = 10

That's curious then: I was having a stack overflow message (I presented here a small generalization of my original problem) and after some investigation I decided to declare"array" as allocatable and the stack overflow message was gone - the code worked. The /heap-arrays option has always been activated. That is why I was wondering whether array would be allocated to the stack... But your answer confused me.

Does the fact that I have lower and upper bounds in array change anything? Also the kind of array is also defined as an integer parameter inside Global_Data... I would guess not, but just checking.

real(KIND=DOUBLE) array(N1:N2,N3:N4)
0 Kudos
Steven_L_Intel1
Employee
1,017 Views
What are your compiler options? The default is that local arrays are statically allocated, but this could change if you added /Qopenmp, /parallel or /recursive.
0 Kudos
rafadix08
Beginner
1,016 Views
What are your compiler options? The default is that local arrays are statically allocated, but this could change if you added /Qopenmp, /parallel or /recursive.

Yes, you are right, I added /Qopenmp, that makes local arrays automatic by default. Thank you!
0 Kudos
Reply