Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26732 Discussions

Stackoverflow when adding allocated arrays

Pedersen__M
Beginner
131 Views

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
Pedersen__M
Beginner
131 Views

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?

 

IanH
Black Belt
131 Views

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.

 

Pedersen__M
Beginner
131 Views

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.

 

 

IanH
Black Belt
131 Views

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

Reply