# \`-frecursive\`, \`-assume recursion\`, and recursion/thread-safety

**URL:** <https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety/6571>\
**Category:** Help\
**Created:** [September 28, 2023, 5:54am UTC](https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety/6571 "2023-09-28T05:54:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 28, 2023, 5:54am UTC](https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety/6571/1 "2023-09-28T05:54:07Z")

</div>

I have the following questions regarding `-frecursive` for `gfortran` and `-assume recursion` for `ifort`.

**Q1.** Does `-frecursive` conflict with `-fno-stack-arrays` for `gfortran`?

According to [Code Gen Options (The GNU Fortran Compiler)](https://gcc.gnu.org/onlinedocs/gfortran/Code-Gen-Options.html)

```auto
-frecursive
    Allow indirect recursion by forcing all local arrays to be allocated on the stack. This flag cannot be used together with -fmax-stack-var-size= or -fno-automatic.

```

But I did not find any documentation about `-fno-stack-arrays`. I tried `gfortran -frecursive -fno-stack-arrays` with `gfortran-13` and received no warning or error.

**Q2.** What is the default behavior of `gfortran` regarding `-frecursive`? Is it `-frecursive` or `-fno-recursive`?

**Q3.** Is `-assume recursion` the `ifort` correspondent of `-frecursive`?

**Q4.** Similar to **Q1** , does `-assume recursion` conflict with `-heap-arrays` for `ifort`?

**Q5.** Is it possible to compile a program so that all **automatic arrays** and **local arrays** are allocated on the heap (to **avoid stack overflows** ) while ensuring **recursion-safety and thread-safety**? If yes, how should we specify the compiler flags for `gfortran` and `ifort`? If no, why? See [a previous related discussion](https://fortran-lang.discourse.group/t/heap-stack-and-thread-safety/4944).

N.B.: I do understand that deep recursion can cause stack overflows. Let us assume that the recursion depth is small, e.g., 2 or 3.

Thank you.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [September 28, 2023, 10:38am UTC](https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety/6571/2 "2023-09-28T10:38:33Z")

</div>

What is important for recursion AND thread safety is to NOT have static local variable. All local variables have to be dynamically allocated upon entering the routine (unless you really know what you are doing, then you can force some variable to be static with the `save` attribute if this is really what you want), but whether they are allocated on the stack or on the heap doesn’t matter.

So my understanding is:

- `-frecursive` forces the local variables to be dynamically allocated, but the compiler has always the choice between the stack and the heap (even if the stack is generally the first choice of the compilers)
- `-fno-stack-arrays` forces all dynamic allocations of arrays to be on the heap and not on the stack

To me, there’s no conflict between these options.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 28, 2023, 10:44am UTC](https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety/6571/3 "2023-09-28T10:44:13Z")

</div>

Hi, @PierU , thank you for the response.

> - `-frecursive` forces the local variables to be dynamically allocated, but the compiler has always the choice between the stack and the heap (even if the stack is generally the first choice of the compilers)
> - `-fno-stack-arrays` forces all dynamic allocations of arrays to be on the heap and not on the stack

Then the following documentation does not seem precise, does it?

> [@zaikunzhang](#):
>
> ```auto
> -frecursive
> Allow indirect recursion by forcing all local arrays to be allocated on the stack.
> 
> ```

Thanks.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [September 28, 2023, 4:56pm UTC](https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety/6571/4 "2023-09-28T16:56:35Z")

</div>

> [@zaikunzhang](#):
>
> Then the following documentation does not seem precise

Probably not.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [September 28, 2023, 5:06pm UTC](https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety/6571/5 "2023-09-28T17:06:10Z")

</div>

For ifort, use `assume recursion` or `-standard-semantics`. You can also use `-heap-arrays` with either of these.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 28, 2023, 5:23pm UTC](https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety/6571/6 "2023-09-28T17:23:43Z")

</div>

Thank you Dr. Fortran @sblionel for the response, which is reassuring.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 29, 2023, 3:25am UTC](https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety/6571/7 "2023-09-29T03:25:49Z")

</div>

Thanks to Dr. Fortran’s explanation, I am 100% confident that `-assume recursion -heap-arrays` is the flag to make automatic arrays and local arrays allocated on the heap while ensuring recursion-safety and thread-safety.

What about `gfortran`? Is `-frecursive --fno-stack-arrays` the answer? Is this combination already the default?

What about other popular compilers like the NAG compiler and various flangs?

Thank you.
