# Automatic allocation of string in recursive function unexpected result \[issue with \`ifx\` and bug in \`gfortran\` and \`nvfortran\`\]

**URL:** <https://fortran-lang.discourse.group/t/automatic-allocation-of-string-in-recursive-function-unexpected-result-issue-with-ifx-and-bug-in-gfortran-and-nvfortran/8775>\
**Category:** Help\
**Created:** [October 30, 2024, 5:43pm UTC](https://fortran-lang.discourse.group/t/automatic-allocation-of-string-in-recursive-function-unexpected-result-issue-with-ifx-and-bug-in-gfortran-and-nvfortran/8775 "2024-10-30T17:43:04Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [November 2, 2024, 4:28pm UTC](https://fortran-lang.discourse.group/t/automatic-allocation-of-string-in-recursive-function-unexpected-result-issue-with-ifx-and-bug-in-gfortran-and-nvfortran/8775/6 "2024-11-02T16:28:21Z")

</div>

I remember a similar problem posted some time ago about the result variable of allocatable character string (with OpenMP, replied by @martin with a link to the bug report 113797)

> [@Integer or real to character string: bug with gfortran with OpenMP?](https://fortran-lang.discourse.group/t/integer-or-real-to-character-string-bug-with-gfortran-with-openmp/8408/6):
>
> This looks very similar to what is reported in [Bug 113797](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113797).

If this may be related, the runtime error I encountered above might also be a result / side effect of the same possible bug… (but here without OpenMP). FWIW, the last part of the above bug report says:

> ```auto
> typedef character(kind=1) struct
> character(kind=1)[1:slen.1][1:slen.1];
> pstr.2 = 0B;
> slen.1 = 0;
> 
> ```
> 
> Maybe the order here. slen.1 should be set to 0 before the use inside the typedef.  
> That is kinda of the reason why static works (with not openmp)

Also FWIW, I’ve tried changing the main program to a subroutine (to avoid the complication of the main program), and “-fsanitize=address” gave a memory leak of \>1300 bytes.

A workaround might be to wrap the allocatable string to a derived type (like “variable string”) and use it throughout (including the result variables), or more conveniently, use some existing library for a wrapped string type, but the possible compiler bug above (not sure yet) remains…

---

_[View the full topic](https://fortran-lang.discourse.group/t/automatic-allocation-of-string-in-recursive-function-unexpected-result-issue-with-ifx-and-bug-in-gfortran-and-nvfortran/8775)._
