I get a stackoverflow when adding two allocated arrays, so it seems to make a temporary array on the stack:
program main integer :: n=160000 integer*8, dimension(:), pointer :: a,b,c allocate(a(n)) allocate(b(n)) allocate(c(n)) a(:)=1 b(:)=2 c = a+b ! forrtl: severe (170): Program Exception - stack overflow end program
It does not fail if I make a loop and add one element at the time.
Is there a better way to avoid this?
The size should be zero (i.e. /heap-arrays:0).
Why are your arrays pointers? Do they need to be? The compiler often has to assume that things that can be aliased (pointers and targets) are aliased (i.e. perhaps `c` is the same things as `a` or `b`)- the creation of temporaries is one of the consequences of this.
Thank you @IanH for the reply.
Is there any overhead when using /heap-arrays:0
I think the reason for using pointers instead of allocatable was that the arrays are declared in a type, and previous compilers did not like that.
/heap-arrays means that storage for temporaries will be on the heap. Setting aside that storage is typically slower than setting aside storage on the stack.
Allocatable components are commonly supported and are a much better solution if you don't need the reference semantics of pointers.