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

Maximum level of recursion in recursive subroutines

suman
Beginner
1,208 Views
Hi all,

I have spent a few days trying to find a bug in my code, but later I realised that ifort comes up with segmentation fault beyond certain level of recursion in recursive subroutines.

Can you please let me know the maximum allowed number? What does that depend on?

The following code does not help me, since the extent to which it will run before it gives 'segmentation fault' will change every time I run it! :O

Test code:

 ! RECURS.F90
!
i = 0
CALL Inc (i)
END

RECURSIVE SUBROUTINE Inc (i)
i = i + 1
CALL Out (i)
IF (i.LT.157500) CALL Inc (i) ! This also works in OUT
END SUBROUTINE Inc

SUBROUTINE Out (i)
WRITE (*,*) i
END SUBROUTINE Out
0 Kudos
1 Reply
Bonnie_A_Intel
Employee
1,208 Views

Recursion pushes a stack frame and arguments for each invocation. So the limit is the limit of your current stacksize. Try the following commands from your system prompt:

ulimit a [to see the current limit]

ulimit s [to set the stack size as unlimited to make it as big as supported on your system]

The limit will vary a little by Linux type and version.

Procedure calls always use the stack to save the return information. The return address is stored along with return results (RESULT) values. The arguments are, by default, also passed on stack.

0 Kudos
Reply