Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
2 Views

Stackoverflow when adding allocated arrays

Hi

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?

 

0 Kudos
4 Replies
Highlighted
Beginner
2 Views

I realized that I can use the

I realized that I can use the compiler flag /heap-arrays[:size]

Is there any drawbacks of using this flag and how big should size be?

 

0 Kudos
Highlighted
Black Belt
2 Views

The size should be zero (i.e.

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.

 

0 Kudos
Highlighted
Beginner
2 Views

Thank you @IanH for the reply

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.

 

 

0 Kudos
Highlighted
Black Belt
2 Views

/heap-arrays means 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.

0 Kudos